Apex

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:

  1. The label of sObject type.
    Example:

    List<SObject> objs = new List<SObject>();
    
    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}

    Contact record appears in list before an Opportunity even though it was added later.

  2. The name field, if present.
    List<SObject> objs = new List<SObject>();
    
    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}

    Opportunities are sorted by Name, ‘opp1’ comes first even though it was entered last.

  3. Standard fields, in alphabetical order (except Id and Name fields)
    List<SObject> objs = new List<SObject>();
    
    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}

    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.

  4. 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:


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;

}

}

}

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.


if(product.Price__c > wrapProduct.Price__c)

return -1;

else if(product.Price__c < wrapProduct.Price__c)

return 1;

else

return 0;

You can use the below snippet to run in developer console and see the result.


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<ProductWrapper> pList = new List<ProductWrapper>();

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

}

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s