Click to See Complete Forum and Search --> : How to sort a collection of object based on a property of the object
shuklamanish15
01-09-2007, 11:26 AM
Hi all,
I need to sort an arraylist of object based on a property of the object.I am displaying the data of arraylist in a table in jsp page.When anyone clicks on the header of any column i need to sort the whole table with respect to that column.Can any one please give me some sample code.
I know we should use coparator for this scenario.But how it sort with respect to a particular filed.
I mean i want the code to be reusable.I shuould just pass the list with the name of property wrt which it has to be sorted always.
HELP PLSSSS
mudelta
01-10-2007, 04:04 PM
hello,
i did some googling:
i guess the easiest way is using the following method:
collections.sort(list,comparator)
http://java.sun.com/j2se/1.5.0/docs/api/java/util/Collections.html#sort(java.util.List,%20java.util.Comparator)
the comparator class is a separate class that implements the comparator interface.
have a look at the following url:
http://www.developer.com/java/article.php/858411
since it is a normal class, you can set a constructor with a variabel.
In this case the property you want to sort to.
shuklamanish15
01-11-2007, 08:04 AM
Thanks for the help.
Actually i solved the issue using Comparable.I made the object which are there in the arraylist to implement Comparable interface.Then i added the logic for comparision in compareTo method.For reference i am putting my code:-
public int compareTo(Object obj){
TSIFalloutCase fcase = (TSIFalloutCase)obj;
int i=0;
switch(this.sortBy){
case 1: i = sortOrder*(filterGroupName.compareTo(fcase.filterGroupName));
break;
case 2:
if(falloutCaseId==fcase.falloutCaseId)i= sortOrder*0;
if(falloutCaseId<fcase.falloutCaseId)i= sortOrder*-1;
if(falloutCaseId>fcase.falloutCaseId)i= sortOrder*1;
break;
case 3: i = sortOrder*(falloutType.compareTo(fcase.falloutType));
break;
case 4: i = sortOrder*(this.getRequestNumber().compareTo(fcase.getRequestNumber()));
break;
case 5: i = sortOrder*(this.getNNSP().compareTo(fcase.getNNSP()));
break;
case 6: i = sortOrder*(this.getONSP().compareTo(fcase.getONSP()));
break;
case 7: i = sortOrder*(reasonCode.compareTo(fcase.reasonCode));
break;
case 8: i = sortOrder*(this.getFalloutTime().compareTo(fcase.getFalloutTime()));
break;
case 9: i = sortOrder*(portedNum.compareTo(fcase.portedNum));
break;
default:i = sortOrder*(this.getFalloutTime().compareTo(fcase.getFalloutTime()));
}
return i;
}
Actually my compareTo method helps to sort the arraylist based on a particular field of the object.So to enable that,before sorting i set all the objects in arraylist by sortBy parameter with value of the field on the basis of which i have to do sorting.Also i have one more parameter in the object which tells about the order in which it has to sort(ascending or descending).
Hope this help anyone who comes across a similar problem.
chazzy
01-12-2007, 06:46 AM
just to be complete, you're going to want to have an instanceof check to see that the object passed in is of the correct type. maybe throw an exception if its not?