[SalesForce] List sorting by custom field

I dont know how to sort a list of SObject Case.

I have a List<Case> newCases which i iterate over in for cycle, where i save Cases to my new List<Case> casesList.

for (Case newCase : newCases) {
            if(newCase.Case_Content__c == '111' && newCase.Status == '24'){
                String cuid = newCase.CUID__c;
                cuidSet.add(cuid);
                casesList.add(newCase);
                casesList.sort(); //TODO sorting by CUID__c
            }
        }

What i need to do is, to sort this new list by field CUID__c. So the Cases with same CUID are together.

Thank you for any help 🙂

Best Answer

You have to implement a Wrapper class with comparable interface for this to happen.

To implement a custom sort order for sObjects in lists, create a wrapper class for the sObject and implement the Comparable interface. The wrapper class contains the sObject in question and implements the compareTo method, in which you specify the sort logic.

Eg:

global class OpportunityWrapper implements Comparable {

    public Opportunity oppy;

    // Constructor
    public OpportunityWrapper(Opportunity op) {
        oppy = op;
    }

    // Compare opportunities based on the opportunity amount.
    global Integer compareTo(Object compareTo) {
        // Cast argument to OpportunityWrapper
        OpportunityWrapper compareToOppy = (OpportunityWrapper)compareTo;

        // The return value of 0 indicates that both elements are equal.
        Integer returnValue = 0;
        if (oppy.Amount > compareToOppy.oppy.Amount) {
            // Set return value to a positive value.
            returnValue = 1;
        } else if (oppy.Amount < compareToOppy.oppy.Amount) {
            // Set return value to a negative value.
            returnValue = -1;
        }

        return returnValue;       
    }
}

And then to Sort just call Sort Method after adding records to your wrapper list

        OpportunityWrapper[] oppyList = new List<OpportunityWrapper>();
        Date closeDate = Date.today().addDays(10);
        oppyList.add( new OpportunityWrapper(new Opportunity(
            Name='Edge Installation',
            CloseDate=closeDate,
            StageName='Prospecting',
            Amount=50000)));
        oppyList.add( new OpportunityWrapper(new Opportunity(
            Name='United Oil Installations',
            CloseDate=closeDate,
            StageName='Needs Analysis',
            Amount=100000)));
        oppyList.add( new OpportunityWrapper(new Opportunity(
            Name='Grand Hotels SLA',
            CloseDate=closeDate,
            StageName='Prospecting',
            Amount=25000)));

        // Sort the wrapper objects using the implementation of the 
        // compareTo method.
        oppyList.sort();

So in your case, the CaseWrapper will compare CUID__c to determine sorting order in the compareTo method.

Src: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_list_sorting_sobject.htm