Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a wrapper class which contains a list of sobjects, i want to have th sorting on the fields of these objects in the wrapper list: The apex code is as follows:

Class jobsWrapper
public JobSuite__Job__c job {get;set;}
public JobSuite__Job_Task__c jobTasks{get;set;}
public JobsWrapper(JobSuite__Job__c objjobs, JobSuite__Job_Task__c objjobTasks)
// if(jobTasks== NULL){jobTasks = new List<Job_Task__c>();}
job = objjobs;
jobTasks = objjobTasks; 

public JobsWrapper()
if(jobTasks== NULL){jobTasks = new JobSuite__Job_Task__c();}
if(job == NULL){job = new JobSuite__Job__c();}


I want to allow sorting the wrapper list by the fields in the job - like client, jobname, jobnumber etc and fields from the task object as taskname, reviseddue dateetc.

How can I do this? Please help, thanks in advance.

share|improve this question

Apex provides a sort method on the List class for sorting. For objects such as this however, you must implement the Comparable interface. Note the Salesforce doc samples indicate you need to make your class 'global' scope, this is no longer true.

public class jobsWrapper implements Comparable
    public Integer compareTo(Object compareTo) 
        jobsWrapper jobsWrapper = (jobsWrapper) compareTo;
        if (job.Name == jobsWrapper.job.Name) return 0;
        if (job.Name > jobsWrapper.job.Name) return 1;
        return -1;        

Then once you put your objects into a list you can use the sort method.

List<jobsWrapper> jobs = new List<jobsWrapper>();
// Add things to the list ...
share|improve this answer
+1 .I liked this answer lot .Thanks Andrew .As a developer i frequently come here to see code snippets and these snippets are valuable for quick reference – Mohith Shrivastava Feb 12 '13 at 8:13
Hi Andrew, i tried my example using your same approach given above but sorting does not happen and compareTo method never called. Am i doing anything wrong? – Baskaran S Apr 16 '14 at 13:38

Ok i was a little bit too slow, but here is my idea (a little bit more specific): You could use the Comparable interface and create a dynamic copareTo method

public Class OuterClass {
   public static String sortBy {get; set;}
   Class jobsWrapper implements Comparable{
      public Integer compareTo(Object compareTo) {
         jobsWrapper compareTo = (jobsWrapper)compareTo;
         if (outerClass.sortBy.equals('clientAsc')){
            return job.client__c.compareTo(compareTo.job.client__c);       
         } else if (outerClass.sortBy.equals('clientDesc')) {
            return job.client__c.compareTo(compareTo.job.client__c) * (-1);       
         } else if (outerClass.sortBy.equals('SomeDateAsc')) {
            if ( return 0;
            if ( > 0) return 1;
            return -1; 
         } else {

So you have to set the static sortBy property and then you can simply use the list.sort() method:

share|improve this answer
Thanks all for the quick reply, I used the comparable inteface and was able to sort the list in the ascending order, but i also want to sort the columns in the descending order, as well i want to sort the list on the date fields too. Please reply, Thanks in advance, Sushma – Sushma Rao Feb 12 '13 at 10:45
I added some more examples. Alternatively you could add another static variable for ascending or descending. – Frederik Feb 12 '13 at 11:07
Nice and tidy :) Pity it has to be a standalone class (inner classes can't have static members if I recall correctly). But needs some null checks or rewrite to use == instead of equals() and - instead of daysBetween() or there will be many annoying null pointer exceptions :) And drop the "global" - they recently fixed it so it can be a normal "public" class & method, I don't expose too much stuff as global if I don't have to, gives nasty ideas to integrators who consume your WSDL ;) – eyescream Feb 12 '13 at 11:22
@Frederik as per Andrew comment, if static is not allowed means, what would be the alternative approach? – Baskaran S Apr 16 '14 at 13:39
Put the static variable out of the inner class to the outer class. I edited the code a little bit to make it clear. – Frederik Apr 22 '14 at 6:10

In addition to Andrew's excellent example above, you can simplify it a bit more if you're comparing based on a String type. For instance, you can invoke the "native" String compareTo method like below:

public class jobsWrapper implements Comparable {
    public Integer compareTo(Object compareTo){
        jobsWrapper jobsWrapper = (jobsWrapper) compareTo;
        String thisName = (jobs == null || String.isEmpty(jobs.Name) ? '' : jobs.Name);
        String thatName = (jobsWrapper == null || jobsWrapper.job == null || String.isEmpty(jobsWrapper.job.Name) ? '' : jobsWrapper.job.Name);
        return thisName.compareTo(thatName);

Note: I added some null checking as well in the above example.

share|improve this answer

Thank you for great solutions. I expand them for my requirements "sort wrapper class by dynamic fields and by ASC/DESC".

    public Integer compareTo(Object compareTo) 
        Boolean ascending = (sortBy == 'ASC');
        AccountWrapper accWrap = (AccountWrapper) compareTo;

        String thisValue = (account == null || String.isEmpty(account.get(sortField)+'') 
                             ? '' : String.valueOf(account.get(sortField)+''));
        String compareValue = (accWrap == null || accWrap.account == null || String.isEmpty(accWrap.account.get(sortField)+'') 
                                 ? '' : String.valueof(accWrap.account.get(sortField)+''));
        Integer compareResult = thisValue.compareTo(compareValue);
        return ascending ? -compareResult : compareResult;
share|improve this answer
Is your post an answer to the above question? Please remove it and post as a comment – Bennie Feb 8 at 13:07

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.