I have parent and child objects. now i want to perform sorting in child records list so Kindly suggest me how is it's possible.

currently i am using wrapper class in which i have used "sobject" refers to parent records and "List<toDisplay> children" refer to Child Records list under parent records.

 public class toDisplay {
            public Boolean isFirstObj {get; set;}
            public sObject sObj {get; set;}
            public String objId {get; set;}
            public List<toDisplay> children {get; set;}
             public Boolean toggleEditSave {get; set;}
             public toDisplay(){
        } // End wrapper class
  • You can simply use order by clause to sort the list while querying the records Mar 11, 2015 at 8:02

2 Answers 2


There are multiple possibilities to sort a list of sObjects. The first and easiest one is a use of the sort() method of the List class. Next one is a bit tricky and it is a Comparable interface.


If you have a list of sObjects you can use a standard method sort().

The List.sort method sorts sObjects in ascending order and compares sObjects using an ordered sequence of steps that specify the labels or fields used.

Assumed your list is a list of sObjects, it would be like this in that case:


It is easy to use but you have almost no way to controll the sort process.
Here you can read about sorting: Sorting Lists of sObjects

Comparable Interface

If you wish to have more flexible and powerful sort algorythm you can use a comparable interface. It adds a custom sort order to the list of sObjects (or other non-primitive types).

Alternatively, you can also implement a custom sort order for sObjects by wrapping your sObject in an Apex class and implementing the Comparable interface

So the interface takes two list elements and compares based on some critera defined by you. And this is realy powerful. In the next example I'll use a field of the sObject as a comparison criteria.

public class ChildObject implements Comparable {
    public child Child__c { get; set; }

    public ChildObject(Child__c child) {
        this.child = child;

    // This method returns an integer, that is the result of the comparison
    public Integer compareTo(Object compareTo) {
        // Defining comparison object
        // child          - is a first object for compare
        // compareToChild - is a next element of the list
        ChildObject compareToChild = (ChildObject)compareTo;

        // Now we can compare two child objects to each other 
        // basen on every sObject field
        if(child.SomeSortField__c > compareToChild.SomeSortField__c) {
            returnValue = 1;
        else {
            returnValue = -1;

// Creating a list of child objects
List<ChildObject> myList = new List<ChildObject>();

for(Child__c child : [Select Id, SomeSortField__c From Child__c]) {

// And finally sorting a list of sObjects.
// At this point the comparable interface will sort our list.

Here an official doc:

Now it is up to you which option you will take. I would propose to implement an comparable interface. It is cool, powerfull and flexible.


I wrote a utility class to help with sorting SObjects lists. It supports traversal of multiple parents.

Sort on Field:

Account[] accs = [SELECT My_Custom_Field__c FROM Account LIMIT 2000];
SortSobs.ascending(accs, Account.My_Custom_Field__c);

Sort on parent(s) field:

List<Contact> entries = [SELECT Name, Account.Name FROM Contact limit 2000];
SortSobs.ascending(entries, SObjectField[]{ Contact.Account, Account.Name });

If performance is critical, I would be careful using this (ex: in triggers on core objects).

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .