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.
Eg:
And then to Sort just call Sort Method after adding records to your wrapper list
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