Sorting of sObjects in Salesforce

Sorting of sObjects in Salesforce

I had a requirement where I had to sort a list of custom object records (Product__c) by a custom text field Group__c alphabetically and then by a currency field Price__c from lowest to highest.

There is an instance method sort() for List collection type, it sorts the items in the list in ascending order. Using this method, we can sort primitive types, SelectOption elements and sObjects. But, I found out, Apex does not allow us to use this built-in method to sort a list of sObjects by any specific field of our choice.

If we write sort() method on a list of sObjects, then Salesforce uses a predefined sequence of comparison steps. These are as follows:

The label of sObject type.
Example:
[sourcecode language=”java”]List objs = new List();Opportunity o1 = new Opportunity(Name=’opp1′);Contact c1 = new Contact(LastName = ‘con4′);

objs.add(o1);

objs.add(c1);

objs.sort();

for(SObject obj : objs){

system.debug(obj);

}

Result :

Contact:{LastName=con4}

Opportunity:{Name=opp1}[/sourcecode]
Contact record appears in list before an Opportunity even though it was added later.

The name field, if present.
[sourcecode language=”java”]List objs = new List();Opportunity o1 = new Opportunity(Name=’opp1′);Opportunity o2 = new Opportunity(Name=’opp2′);

Opportunity o3 = new Opportunity(Name=’opp3′);

objs.add(o3);

objs.add(o2);

objs.add(o1);

objs.sort();

for(SObject obj : objs){

system.debug(obj);

}

Result:

Opportunity:{Name=opp1}

Opportunity:{Name=opp2}

Opportunity:{Name=opp3}[/sourcecode]
Opportunities are sorted by Name, ‘opp1’ comes first even though it was entered last.

Standard fields, in alphabetical order (except Id and Name fields)
[sourcecode language=”java”]List objs = new List();Opportunity o1 = new Opportunity(Name=’opp1’,Amount=300, StageName = ‘Qualification’);Opportunity o2 = new Opportunity(Name=’opp1′,Amount=300, StageName = ‘Prospecting’);

Opportunity o3 = new Opportunity(Name=’opp1′,Amount=200, StageName = ‘Value Proposition’);

objs.add(o1);

objs.add(o2);

objs.add(o3);

objs.sort();

for(SObject obj : objs){

system.debug(obj);

}

Result:

Opportunity:{Name=opp1, Amount=200, StageName=Value Proposition}

Opportunity:{Name=opp1, Amount=300, StageName=Prospecting}

Opportunity:{Name=opp1, Amount=300, StageName=Qualification}[/sourcecode]
All the opportunities have same name, so these are sorted by Amount then and followed by StageName, record with ‘Prospecting’ appears before ‘Qualification’ even though added to the list later.

Custom fields, in alphabetical order
Note:

Empty/blank fields appear before non-empty fields in sort order.

So, the question was, ‘How to achieve the functionality’.

Salesforce provides an interface called Comparable that can be used for sorting of custom class/wrapper class. The wrapper class needs to implement the Comparable interface and should contain a method named ‘compareTo’.

CompareTo method:

The syntax for this special method is –

public/global Integer compareTo(Object obj) {

//code to compare the values

}

Values returned:

positive value (usually 1 is used) – current value greater than compared value

negative value (usually -1 is used) – current value lesser than compared value

0 – current value and compared value are equal

Example:
[sourcecode language=”java”]

Public Class ProductWrapper implements Comparable{

Public Product__c product;

//constructor

Public ProductWrapper(Product__c prod){

this.product = prod;

}

public Integer compareTo(Object obj){

ProductWrapper wrapProduct = (ProductWrapper) obj;

If(product.Group__c > wrapProduct.Group__c)

return 1;

else if(product.Group__c < wrapProduct.Group__c) return -1; else { if(product.Price__c > wrapProduct.Price__c)

return 1;

else if(product.Price__c < wrapProduct.Price__c) return -1; else return 0; } } }[/sourcecode] Explanation: These are the records available on Product__c object. If I use the above custom sort with wrapper class I get the below result first order by Group alphabetically, then by Price lowest to highest. A question for you, if I have to order the result by Group alphabetically then by Price from highest to lowest, how could I achieve this? Simple, all I had to do is to return -1 when current price is greater than compared price. [sourcecode language=”java”] if(product.Price__c > wrapProduct.Price__c)

return -1;

else if(product.Price__c < wrapProduct.Price__c) return 1; else return 0;[/sourcecode] You can use the below snippet to run in developer console and see the result. [sourcecode language=”java”] public Class ProductWrapper implements Comparable{ Public Product__c product; Public ProductWrapper(Product__c prod){ this.product = prod; } public Integer compareTo(Object obj){ ProductWrapper wrapProduct = (ProductWrapper) obj; if(product.Group__c > wrapProduct.product.Group__c)

return 1;

else if(product.Group__c < wrapProduct.product.Group__c) return -1; else { if(product.Price__c > wrapProduct.product.Price__c)

return -1;

else if(product.Price__c < wrapProduct.product.Price__c)

return 1;

else

return 0;

}

}

}

List pList = new List();

for(Product__c p : [ SELECT Name, Group__c, Price__c FROM Product__c]) {

pList.add(new ProductWrapper(p));

}

pList.sort();

for(ProductWrapper p : pList){

system.debug(p);

}[/sourcecode]

Leave a Comment

Your email address will not be published. Required fields are marked *

Recent Posts

Salesforce CRM Insights
From Data to Action: Tableau Agent’s Role in Shaping Salesforce CRM Insights
future-of-ai-powered-business-solutions
Dreamforce 2024: Unveiling the Future of AI-Powered Business Solutions
gitex 2024 worlds largest tech show
GITEX 2024 |  World's Largest Tech Show - ABSYZ
inclusive practices in women leadership equitable future
Inclusive Practices in Women’s Leadership: Equitable Future
salesforce dreamfest 2024 the highlights of dreamforce 2024
Dreamfest 2024: The highlights of Dreamforce 2024
Document

How can i help you? close button

powered     by   ABSYZ
Scroll to Top