Wednesday, December 11, 2013

Salesforce : Sorting a wrapper list using Comparable Interface

The Comparable interface adds sorting support for Lists that contain non-primitive types, that is, Lists of user-defined types.

To add List sorting support for your Apex class, you must implement the Comparable interface with its compareTo method in your class.

When a class implements the Comparable interface it has to implement a method called compareTo(). In that method the code has to be written to decide if two objects match or if one is larger than the other.

Apex Class

public class AccountHistoryController {

    public List<AccountHistoryWrapper> accountHistoriesWrapList {get; set;}

    //Calling cosnturctor
    public AccountHistoryController() {
 
        //Memory Allocation
        accountHistoriesWrapList = new List<AccountHistoryWrapper>();
     
        //Loop through Accounts
        for(Account acc : [Select Id, Name, (Select ID, Field, AccountId, CreatedDate, Createdby.Name,                                                       Oldvalue,Newvalue, Account.Name From Account.Histories
                                                Where Field = 'Name' ORDER BY CreatedDate DESC LIMIT 200)
                                                FROM Account]) {
            //Populate wrapper list with values
            accountHistoriesWrapList.add(new AccountHistoryWrapper(acc, acc.Histories));                                      
                                                             
        }
     
        //Get List of wrapper and sort it
        accountHistoriesWrapList.sort();
    }

}

Wrapper Class

global class AccountHistoryWrapper implements Comparable {
 
    public Account account {get; set;}
    public List<AccountHistory> accountHistories {get; set;}
 
    //Calling Constructor
    global AccountHistoryWrapper(Account account, List<AccountHistory> accountHistories) {
        this.account = account;
        this.accountHistories = accountHistories;
    }

     // Compare opportunities based on the opportunity amount.
    global Integer compareTo(Object compareTo) {
     
        // Cast argument to AccountHistoryWrapper
        AccountHistoryWrapper aHW = (AccountHistoryWrapper)compareTo;
     
        // The return value of 0 indicates that both elements are equal.
        Integer returnValue = 0;
        if (account.Name > aHW.account.Name) {
            // Set return value to a positive value.
            returnValue = 1;
        } else if (account.Name < aHW.account.Name) {
            // Set return value to a negative value.
            returnValue = -1;
        }
     
        return returnValue;    
    }

}

Visualforce Page

<apex:page controller="AccountHistoryController">
    <apex:form >
        <apex:pageBlock >
            <apex:repeat value="{!accountHistoriesWrapList}" var="accountHistoryWrap">
                {!accountHistoryWrap.account.Name}
                <BR></BR>
                <apex:pageBlockTable value="{!accountHistoryWrap.accountHistories}" var="aHW">
               
                    <!-- Content of Table-->
                    <apex:column style="width:50%">
                        <apex:facet name="header">Activity</apex:facet>
                        Changed Grade from <b>"{!aHW.oldvalue}"</b> to <b>"{!aHW.newvalue}"</b>
                    </apex:column>
     
                    <!--More Information-->
                    <apex:column headerValue="Date" value="{!aHW.createddate}" style="width:40%"/>
                    <apex:column headerValue="Changed By" value="{!aHW.createdby.Name}" style="width:10%"/>
           
                 </apex:pageBlockTable>
            </apex:repeat>
        </apex:pageBlock>
    </apex:form>
</apex:page>