4

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));
                wrappers.sort();
                sessions.clear();
                for (SessionWrapper w : wrappers) sessions.add(w.sob);
            }
            System.debug('sessionByDay--> '+ twrap.sessionByDay);
        }
2

1 Answer 1

7

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 = ...;
sessions.sort();

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

PS

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));
            wrappers.sort();
            session.clear();
            for (SessionWrapper w : wrappers) session.add(w.sob);
       }
       return session;
   }
    set;
}

PPS

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));
    wrappers.sort();
    sessions.clear();
    for (SessionWrapper w : wrappers) sessions.add(w.sob);
}
6
  • Thanks @Keith for the reply I am updating the wrapper class. Actually I had neglected some of the variables from it. Commented Mar 2, 2016 at 12:59
  • I want to sort list inside the wrapper class Commented Mar 2, 2016 at 13:01
  • @SFDCshaikh See the PS.
    – Keith C
    Commented Mar 2, 2016 at 13:08
  • How it will sort y Start_time__c field? Commented Mar 2, 2016 at 14:55
  • @SFDCshaikh By comparing those fields in the compareTo method that gets called when the list sort method is called.
    – Keith C
    Commented Mar 2, 2016 at 14:57

You must log in to answer this question.

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