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
Define one Enum Type as
public enum Colors {
BLUE, SILVER, MAGENTA, RED
}
Change data type of color
from String
to Colors
Change return type and argument type of getter and setter method of color to Colors
Define comparator type as follows
static class ColorComparator implements Comparator<CarSort>
{
public int compare(CarSort c1, CarSort c2)
{
return c1.getColor().compareTo(c2.getColor());
}
}
after adding elements to List, call sort method of Collection by passing list and comparator objects as arguments
i.e, Collections.sort(carList, new ColorComparator());
then print using ListIterator
.
full class implementation is as follows:
package test;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.ListIterator;
public class CarSort implements Comparable<CarSort>{
String name;
Colors color;
public CarSort(String name, Colors color){
this.name = name;
this.color = color;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Colors getColor() {
return color;
}
public void setColor(Colors color) {
this.color = color;
}
//Implement the natural order for this class
public int compareTo(CarSort c)
{
return getName().compareTo(c.getName());
}
static class ColorComparator implements Comparator<CarSort>
{
public int compare(CarSort c1, CarSort c2)
{
return c1.getColor().compareTo(c2.getColor());
}
}
public enum Colors {
BLUE, SILVER, MAGENTA, RED
}
public static void main(String[] args)
{
List<CarSort> carList = new ArrayList<CarSort>();
List<String> sortOrder = new ArrayList<String>();
carList.add(new CarSort("Ford Figo",Colors.SILVER));
carList.add(new CarSort("Santro",Colors.BLUE));
carList.add(new CarSort("Honda Jazz",Colors.MAGENTA));
carList.add(new CarSort("Indigo V2",Colors.RED));
Collections.sort(carList, new ColorComparator());
ListIterator<CarSort> itr=carList.listIterator();
while (itr.hasNext()) {
CarSort carSort = (CarSort) itr.next();
System.out.println("Car colors: "+carSort.getColor());
}
}
}
How about this:
List<String> definedOrder = // define your custom order
Arrays.asList("Red", "Green", "Magenta", "Silver");
Comparator<Car> comparator = new Comparator<Car>(){
@Override
public int compare(final Car o1, final Car o2){
// let your comparator look up your car's color in the custom order
return Integer.valueOf(
definedOrder.indexOf(o1.getColor()))
.compareTo(
Integer.valueOf(
definedOrder.indexOf(o2.getColor())));
}
};
In principle, I agree that using an enum
is an even better approach, but this version is more flexible as it lets you define different sort orders.
Update
Guava has this functionality baked into its Ordering class:
List<String> colorOrder = ImmutableList.of("red","green","blue","yellow");
final Ordering<String> colorOrdering = Ordering.explicit(colorOrder);
Comparator<Car> comp = new Comparator<Car>() {
@Override
public int compare(Car o1, Car o2) {
return colorOrdering.compare(o1.getColor(),o2.getColor());
}
};
This version is a bit less verbose.
Update again
Java 8 makes the Comparator even less verbose:
Comparator<Car> carComparator = Comparator.comparing(
c -> definedOrder.indexOf(c.getColor()));
Comparator in line ...
List<Object> objList = findObj(name);
Collections.sort(objList, new Comparator<Object>() {
@Override
public int compare(Object a1, Object a2) {
return a1.getType().compareToIgnoreCase(a2.getType());
}
});
I think this can be done as follows:
class ColorComparator implements Comparator<CarSort>
{
private List<String> sortOrder;
public ColorComparator (List<String> 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));
I will do something like this:
List<String> order = List.of("Red", "Green", "Magenta", "Silver");
Comparator.comparing(Car::getColor(), Comparator.comparingInt(c -> order.indexOf(c)))
All credits go to @Sean Patrick Floyd :)
I recommend you create an enum for your car colours instead of using Strings and the natural ordering of the enum will be the order in which you declare the constants.
public enum PaintColors {
SILVER, BLUE, MAGENTA, RED
}
and
static class ColorComparator implements Comparator<CarSort>
{
public int compare(CarSort c1, CarSort c2)
{
return c1.getColor().compareTo(c2.getColor());
}
}
You change the String to PaintColor and then in main your car list becomes:
carList.add(new CarSort("Ford Figo",PaintColor.SILVER));
...
Collections.sort(carList, new ColorComparator());