You need to sign in to do that
Don't have an account?
How to sort wrapper class list ?
Hi all,
I have a wrapper class which contains a list of sobjects, i want to have th sorting on the fields of these objects in the wrapper list:
The apex code is as follows:
Class jobsWrapper
{
public JobSuite__Job__c job {get;set;}
public JobSuite__Job_Task__c jobTasks{get;set;}
public JobsWrapper(JobSuite__Job__c objjobs, JobSuite__Job_Task__c objjobTasks)
{
// if(jobTasks== NULL){jobTasks = new List<Job_Task__c>();}
job = objjobs;
jobTasks = objjobTasks;
}
public JobsWrapper()
{
if(jobTasks== NULL){jobTasks = new JobSuite__Job_Task__c();}
if(job == NULL){job = new JobSuite__Job__c();}
}
}
i want to allow sorting the wrapper list by the fields in the job - like client, jobname, jobnumber etc and fields from the task object as taskname, reviseddue dateetc.
How can i do this?
Please help, thanks in advance.
Hi Sushma,
There are some ways to sort the list of Sobject.
1. You can make a query using the order by keyword and assign the result to the list
- If the list is formed already, then you can take the record Ids from that list into a set and in the query mention "where Id IN : set AND order by Name/..."
2. you can add the records into a map. The map should be like : key as the Field (on which field basis you want to sort) and the value is SObject
- Now the map is formed and the keyset of the map can be sorted . When keys are sorted obviously the values also will get sorted.
- Now take the map values into a list.
Thus you can get the sorted list.
Thanks harsha for the reply,
I have two objects in the list job and job task. Job is master and the job task is detail. In the query i am getting the records for the job, then in another query i am getting the job tasks for each job and i have a function to create the wrapper and add these two object, i am adding one job and then the task for the job which has the latest revised due date.
Sorting for the toher columns is fin but for the task columns if i sort in the query then i get the tasks with not the which has the latest revised due date. How should i handle this?
The function for the wrapper add is as follows:::
Please help thanks, Thanks in advance.
I think there is better way- using Comparable interface.
In your case it'll be something like this:
public static String compareField {get; set;} //let the user to update this field from the page
Class jobsWrapper implements Comparable
{
public JobSuite__Job__c job {get;set;}
public JobSuite__Job_Task__c jobTasks{get;set;}
public JobsWrapper(JobSuite__Job__c objjobs, JobSuite__Job_Task__c objjobTasks)
{
// if(jobTasks== NULL){jobTasks = new List<Job_Task__c>();}
job = objjobs;
jobTasks = objjobTasks;
}
public JobsWrapper()
{
if(jobTasks== NULL){jobTasks = new JobSuite__Job_Task__c();}
if(job == NULL){job = new JobSuite__Job__c();}
}
public Integer compareTo(Object jobWrp)
{
jobsWrapper comparejobWrp = (jobsWrapper)jobWrp;
if((String)job.get(compareField) > (String)compareToRL.job.get(compareField))
1;
else
0;
}
}
Ohk.
Here the second way will help you (from the 2 ways I suggested in my earlier post).
See, however one wrapper class instance is having both job and it's task. And the wrapper instance is added to the list.
Iterate through that list and put the values into the map as followed.
- Key as the field that you wanted to get sorted
- Value will be the wrapper class instance i.e. the listWrapper[0], [1], [2], ....
Now you can sort the keyset of the map and obviously the values are also ordered in the same way as of the keys.
Finally add all the map values to the list if wrapper.
Thanks all for the quick reply,
I used the comparable inteface and was able to sort the list in the ascending order, but i also want to sort the columns in the descending order, as well i want to sort the list on the date fields too.
How can I sort the list in the descending order as well as on the date fields?
Please reply,
Thanks in advance.
Hey, can you please tell me what is the comparable interface and how to use it?
You'll need another variable in your controller , e.g. SortOrder, and change it from 'asc' to 'desc' and vice versa each time.
Than, in the compareTo function you can write:
if((String)job.get(compareField) > (String)compareToRL.job.get(compareField))
return SortOrder.equals('asc') ? 1 : 0;
else
return SortOrder.equals('asc') ? 0 : 1;
Mad Developer, The comparable interface is the same as in java.
you can read here:
http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/Comparable.html
Thanks lironand I am Toddman.
Thanks all for the reply, but i am still not able to do the sorting as my wrapper class the variables are static they will not be updated with a new value in the sorting, my code for the sorting is as follows:
The problem is I am sorting the collumns by ascending at frst then on click on the collumn to sort the collumns in the descending order.
Please suggest some way to do this.
Thanks in advance.
hi,
looks a little "messi".
When I did it I use only 2 variable:
public static String comparedField {get; set;}
public static String sortOrder {
get {
if(sortOrder== null)
sortOrder='asc';
return sortOrder;
}
set;
}
I change both of them when the user click on column header in the page.