function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
HilineHiline 

How to show sorted aggregate results on a VF page

I'm trying to show the sorted aggregate results on a VF page.  One of the wrapper class properties "SipPct" is calcuated by using the two aggregate result fields.  Is there an way to sort the results by "SipPct"?   Below is controller code:

<pre>
public with sharing class TestController {
    public Summary[] Summaries { get; set; }
    public TestController() {
        AggregateResult[] results = [
            SELECT Opportunity.Owner.Name, Avg(Opportunity.Owner.SIP__c), sum(TotalPrice)
            FROM OpportunityLineItem
            WHERE Opportunity.CloseDate = THIS_YEAR and
            Group by Opportunity.Owner.Name
        ];
        Summaries = new List<Summary>();
        for (AggregateResult ar : results) {
            Summaries.add(new Summary(ar));
        }
      
    }

    // wrapper class to hold aggregate data
    public class Summary {
        public Double Sip { get; private set; }
        public String Name { get; private set; }
        public Double TotalPrice { get; private set; }
        public Double SipPct { get; private set; }

        public Summary(AggregateResult ar) {
            Name = (String) ar.get('Name');
            Sip = (Double) ar.get('expr0');
            TotalPrice = (Double) ar.get('expr1');
            SipPct = TotalPrice / Sip;
        }
  }
}
</pre>
sfdcfoxsfdcfox
If you implement the Comparable interface on the object, you can then proceed to sort the list automatically. This is in the documentaiton located here:

http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_System_List_sort.htm

A more detailed example is located in the documentation here:

http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_list_sorting_sobject.htm