Solved

How to I custom sort multiple fields in SalesForce Apex

Posted on 2014-01-13
2
521 Views
Last Modified: 2016-06-13
In a Salesforce Apex class how can we sort a list of custom objects by multiple fields asc or desc?  I have a need to sort based on certain conditions from within an apex trigger.  I cannot re-query the data to sort using a soql query because it hits a query limit.
 
I know half the answer to this question.  Create a wrapper class http://salesforce.stackexchange.com/questions/8370/how-to-sort-wrapper-list .  Just have to figure out how to do it for multiple fields.  Also, I’m hoping there is an easier way.
0
Comment
Question by:heykjo
2 Comments
 
LVL 1

Accepted Solution

by:
Jamie Browning earned 500 total points
ID: 40144382
First of all I would establish your sorting rules.
Then rewrite something like this:

public class jobsWrapper implements Comparable
{
    public Integer compareTo(Object compareTo)
    {
        jobsWrapper jobsWrapper = (jobsWrapper) compareTo;
        if (job.Name == jobsWrapper.job.Name && job.Custom__c == jobsWrapper.job.Custom__c) return 0;
        if (job.Name == jobsWrapper.job.Name && job.Custom__c > jobsWrapper.job.Custom__c) return 1;
        if (job.Name == jobsWrapper.job.Name && job.Custom__c < jobsWrapper.job.Custom__c) return -1;
        if (job.Name > jobsWrapper.job.Name) return 1;
        return -1;        
    }
}
The Same as the code provided but has additional levels of Logic.
Not sure what limits you were previously hitting but this may at least give you a start.
To add further fields use the same pattern I have applied with and && logical and and further == , > , < expressions.

I hope this helps.
I can provide more if you need.

Jamie
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Salesforce.com is a cloud-based customer relationship management (CRM) system. In this article, you will learn how to add and map custom lead and contact fields to your Salesforce instance.
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question