[SalesForce] Sorting a list of sObjects

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(){
                this.toggleEditSave=false;
            }
        } // End wrapper class

Best Answer

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.

List.sort()

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:

children.sort();

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]) {
    myList.add(child);
}

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

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.