+ Start a Discussion

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:

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;
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:


A more detailed example is located in the documentation here: