Solved

How to I custom sort multiple fields in SalesForce Apex

Posted on 2014-01-13
2
555 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
index Out OF Range Exception error 4 70
Adoquery sql  left join does not work 25 99
Query Syntax 17 43
asp Google Map 2 32
Messaging apps are amazing tools with the power to do a lot of good, but the truth is the process of collaborating with coworkers requires relationships established through meaningful communication - the kind of communication that only happens face-…
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

828 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