Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have Wrapper class which contains List of Map. After performing some operations that List values order gets changed.

public class TeacherAvailabilityWrapper{
    public List<Session__c> session{get;set;}
    public Set<Date> dateOnCalender{get;set;}
    public Map<Date,List<Session__c>> dateSession{set;get;}
    public Map<String,List<Session__c>> sessionByDay{set;get;}

    public Integer srno{get;set;}
    public String Teacher{get;set;}
    public Id IdTeacher{get;set;}
    public TeacherAvailabilityWrapper(){
       session = new List<Session__c>();

Now I want to sort List of dateSession. Based on Start_time__c (DateTime)field in session.

How to achieve it using Comparable interface?

applied PPS logic of @Keith C enter image description here

I added following code

for(TeacherAvailabilityWrapper twrap: listTeacherAvailWrap){
            for(Date twDt: twrap.dateSession.KeySet()){
                Session__c[] sessions = twrap.dateSession.get(d);
                SessionWrapper[] wrappers = new SessionWrapper[] {};
                for (Session__c s : sessions) wrappers.add(new SessionWrapper(s));
                for (SessionWrapper w : wrappers) sessions.add(w.sob);
            System.debug('sessionByDay--> '+ twrap.sessionByDay);
share|improve this question
have you tried with comparable Interface ? – Ratan Mar 2 at 12:26
I am not getting from where to start for comparable interface. – SFDC shaikh Mar 2 at 12:27
up vote 7 down vote accepted

The confusion is that your class is not really what is termed a wrapper in the Apex community as a wrapper usually decorates a single object instance not a collection. Best drop that suffix from your class.

A wrapper that allows easy sorting (see Comparable) by the field you specify (assuming the field is never null) would look like this:

public class SessionWrapper implements Comparable {
    public Session__c sob;
    public SessionWrapper(Session__c sob) {
        this.sob = sob;
    public Integer compareTo(Object o) {
        SessionWrapper that = (SessionWrapper) o;
        if (this.sob.Start_time__c < that.sob.Start_time__c) return -1;
        else if (this.sob.Start_time__c > that.sob.Start_time__c) return 1;
        else return 0;

and if you then have a list of these objects you can sort like this:

List<Sessionrapper> sessions = ...;

But you would need to re-design your class to fit this into your logic.


This would do the sort any time the session field is referenced if you think about when the session objects are added and accessed and only do the sort when needed you would avoid wasted compution:

public List<Session__c> session {
    get {
        if (session != null) {
            SessionWrapper[] wrappers = new SessionWrapper[] {};
            for (Session__c s : session) wrappers.add(new SessionWrapper(s));
            for (SessionWrapper w : wrappers) session.add(w.sob);
       return session;


The lists in your code are separate so sorting one list does not sort any of the other lists. So if you need you dateSession lists sorting, just before you display them in your page run this code:

for (Date d : dateSession.keySet()) {
    Session__c[] sessions = dateSession.get(d);
    SessionWrapper[] wrappers = new SessionWrapper[] {};
    for (Session__c s : sessions) wrappers.add(new SessionWrapper(s));
    for (SessionWrapper w : wrappers) sessions.add(w.sob);
share|improve this answer
Thanks @Keith for the reply I am updating the wrapper class. Actually I had neglected some of the variables from it. – SFDC shaikh Mar 2 at 12:59
I want to sort list inside the wrapper class – SFDC shaikh Mar 2 at 13:01
@SFDCshaikh See the PS. – Keith C Mar 2 at 13:08
How it will sort y Start_time__c field? – SFDC shaikh Mar 2 at 14:55
@SFDCshaikh By comparing those fields in the compareTo method that gets called when the list sort method is called. – Keith C Mar 2 at 14:57

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.