Activity Forums Salesforce® Discussions How can I Sort Wrapper list in Salesforce? Reply To: How can I Sort Wrapper list in Salesforce?

  • Parul

    September 22, 2018 at 5:25 am

    Adding some points and code snippet:

    37down vote
    Apex provides a sort method on the List class for sorting. For objects such as this however, you must implement the Comparable interface. Note the Salesforce doc samples indicate you need to make your class 'global' scope, this is no longer true.

    public class jobsWrapper implements Comparable
    public Integer compareTo(Object compareTo)
    jobsWrapper jobsWrapper = (jobsWrapper) compareTo;
    if (job.Name == jobsWrapper.job.Name) return 0;
    if (job.Name > jobsWrapper.job.Name) return 1;
    return -1;
    Then once you put your objects into a list you can use the sort method.

    List<jobsWrapper> jobs = new List<jobsWrapper>();
    // Add things to the list ...