Solved

How to I custom sort multiple fields in SalesForce Apex

Posted on 2014-01-13
2
677 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Get to know the ins and outs of building a web-based ERP system for your enterprise. Development timeline, technology, and costs outlined.
For cloud, the “train has left the station” and in the Microsoft ERP & CRM world, that means the next generation of enterprise software from Microsoft is here: Dynamics 365 is Microsoft’s new integrated business solution that unifies CRM and ERP fun…
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…

726 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