How to sort list items for Date field value (Apex, Salesforce)

Posted by Channa on Stack Overflow See other posts from Stack Overflow or by Channa
Published on 2012-03-22T05:24:18Z Indexed on 2012/03/22 5:30 UTC
Read the original article Hit count: 256

Filed under:

With Salesforce's Apex, is there any way to sort list items, for Date field value. Please refer the TODO section of the following code, thanks.

/**
 * Select list of jobOccurrences belongs to particular list of jobs
 */
private List<Job_Occurrence__c> getJobsByJobCode(List<Job__c> jobList) {

 // Select relevant Job Occurrence objects 
    List<Job_Occurrence__c> jobOccuList = new List<Job_Occurrence__c>();

    for (Job__c job : jobList) {  

         Job_Occurrence__c jobOccurrence = [SELECT Id, Job__c,  
                                                   Schedule_Start_Date__c 
                                            FROM Job_Occurrence__c 
                                            WHERE Job__c =: job.Id];

         if((jobOccurrence != null) && (jobOccurrence.Id != null)) {
            jobOccuList.add(jobOccurrence);
         }        
    }   

    if((jobOccuList != null) && (jobOccuList.size() > 0)) {

       // TODO
       // How I sort the 'jobOccuList' with Date field 'Schedule_Start_Date__c',   
       // for select the items according to sequence of latest jobOccurrence    

       return jobOccuList; 

    } else {
       throw new RecordNotFoundException ('Could not found any jobOccurrence for given list of jobs');   
    }                            
}

© Stack Overflow or respective owner

Related posts about apex-code