I want to develop a sorting demo for car list. I am using data table to display car list. Now actually I want to sort the list by car color. Here it is not sort by alphabeti
I think this can be done as follows:
class ColorComparator implements Comparator
{
private List sortOrder;
public ColorComparator (List sortOrder){
this.sortOrder = sortOrder;
}
public int compare(CarSort c1, CarSort c2)
{
String a1 = c1.getColor();
String a2 = c2.getColor();
return sortOrder.indexOf(a1) - sortOrder.indexOf(a2);
}
}
For sorting use this:
Collections.sort(carList, new ColorComparator(sortOrder));