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.


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 ...
| improve this answer | |
  • 4
    +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? – Bforce 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 (job.date.isSameDay(compareTo.job.date)) return 0;
            if (job.date.daysBetween(compareTo.job.date) > 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:

| 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
  • 1
    I added some more examples. Alternatively you could add another static variable for ascending or descending. – Frederik Feb 12 '13 at 11:07
  • 2
    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? – Bforce 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.

| improve this answer | |

Wrapper list Sorting.

For Example this wrapper list ( List allcWraplist = new List()) Now we sort the above wrapper list

                        // Add Method for Wrapper Sorting where you define wrapper class attibute
                        public Integer compareTo(Object compareTo)
                            AllocationWrapper compareToGoal = (AllocationWrapper)compareTo;

                            if(type < compareToGoal.type)
                            return 1;
                            else if (type < compareToGoal.type)
                            return -1;
                            return 0;

                      Now we Use for Sorting 
                      allcWraplist.sort(); //Implement compareTo for Wrapper List Sorting 
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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