Take the 2-minute tour ×
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.

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

2 Answers 2

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 ...
jobs.sort();
share|improve this answer
2  
+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 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:

OuterClass.sortBy='client';
jobWrapperList.sort();
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
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? –  Baskaran S Apr 16 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 at 6:10

Your Answer

 
discard

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.