Solved

How to I custom sort multiple fields in SalesForce Apex

Posted on 2014-01-13
2
445 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
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
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 …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

772 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now