How to sort list items for Date field value (Apex, Salesforce)
- by Channa
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');
}
}