I have created one vf page on which I am showing records from parent object along with one date field on child object. For eg. Job(parent) and its taks(child), I am showing job details with one of its latest task's date. Now I wanted to sort this list of records by the task date on child object. How can I do that?

Thank you in advance.


If I were you I would use:

Wrapapper class:


and comparable interface:


Or If it Parent object is master you could create roll-up summary field.

  • Hi Artur, I am just editing the existing code as this page was previously coded by my colleague, its showing parent record on a page in tabular format along with that I am showing date from one of the child record for that parent record. For eg. Job1 is parent record and it have task1, task2, task3(child records). Then I am showing job1 fields and task1's date in a row. like that there will be job list and I want to sort the list by child object's date field. So instead of editing whole code I wanted to sort it. Do you know anyway to do that? Else, I can edit whole page with new logic. Thanx. May 8 '14 at 6:55
  • my query, " SELECT [fields], (SELECT [fields] FROM child_object) FROM parent_object WHERE some condition ORDER BY [some criteria] ". I wanted to know is there any way to add child object field in "ORDER BY" section. May 8 '14 at 6:57
  • You can't use ORDER BY in that way. But you can implement comparable - it is quite easy - you will have 1 methode more in code. Or if this relation is master-detail you can create custom roll-up field on parent and use it in Order By. May 8 '14 at 7:05

Another possibility is to reorder the list by querying the child object using the MAX aggregate function, ordering by the MAX date. This could be useful if you cannot or do not want to refactor other code to use a wrapper class and comparable.

This code assumes that you already have a List of Parent__c objects, along with the related child list containing the single record that has the latest date. The AggregateResult query selects the maximum date for each parent and orders the results by that maximum in descending order (i.e., greatest date first).

// This already exists somehow
List<Parent__c> theParents = getParentsAndSingleChildRecSomehow();

theParents = sortByChildDate(theParents);

private List<Parent__c> sortByChildDate(List<Parent__c> parents) {
    Map<Id, Parent__c> parentMap = new Map<Id, Parent__c>(parents);
    List<Parent__c> retList = new List<Parent__c>();

    for (AggregateResult aggRes : [
         SELECT MAX(Date__c), Parent__c parentId
         FROM Child__c
         WHERE Parent__c IN :parentMap.keySet()
         GROUP BY Parent__c
         ORDER BY MAX(Date__c) DESC
    ]) {
        Id parentId = (Id) aggRes.get('parentId');
    return retList;

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.