function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Parteek Kumar 5Parteek Kumar 5 

Urgent: How to use sorting in wrapper list of task

Hi All,

I am using a visualforce page for showing task and lead data using wrapper class. In this page I have to use sorting according to priority and activity date. i am not able to understand how to achieve this.
below is my code...
 
public class wrapper implements Comparable
    {
        public decimal st{get;set;}
        public task t{get;set;}
        public string wid{get;set;}
        public wrapper(decimal s,task td,string wi)
        {
           st=s;
           t=td;
           wid=wi;
        }
        public Integer compareTo(Object compareTo) 
        {
        
            wrapper compareToEmp = (wrapper)compareTo;
            
            if(t.priority==compareToEmp.t.priority)
            { return 0;}
            else if(t.priority>compareToEmp.t.priority) 
            {return 1;}
            
            else
            {
                if(t.activitydate==compareToEmp.t.activitydate) return 0;
                if (t.activitydate < compareToEmp.t.activitydate) return 1;
            }
            
            return -1;

            
        }
        

        
         
     }

Please help me guys....
 
pconpcon
Can you expand upon what is not working correctly?  I think there may be a flaw in your logic.  On line 17 you have that if they have the same priorty then they should be considered equal.  I think you want to fall back to your date if they have the same priority.  Correct?  If so, the updated code below will sort by priority first then fall back to activity date.
 
public class wrapper implements Comparable {
    public Decimal st {
        get;
        set;
    }
    
    public Task t {
        get;
        set;
    }

    public String wId {
        get;
        set;
    }

    public Wrapper(Decimal st, Task t, String wId) {
        this.st = st;
        this.t = t;
        this.wId = wId;
    }

    public Integer compareTo(Object compareTo) {
        Wrapper compareToEmp = (Wrapper) compareTo;

        if (this.t.priority < compareToEmp.t.priority) {
            return -1;
        } else if (this.t.priority > compareToEmp.t.priority) {
            return 1;
        } else if (this.t.activitydate < compareToEmp.t.activitydate) {
            return -1;
        } else if (this.t.activitydate > compareToEmp.t.activitydate) {
            return 1;
        }
        
        return 0;
    }   
}
NOTE: This code has not been tested and may contain typographical or logical errors