I want to create a custom comparator in java that can take different types of classes and sort by a particular method (which will be the same across all the classes). These classes are not related to each other and inheritance can't be applied to them.
For instance, suppose I have 2 classes Car and Bike, and both of them have a getter method which gives the count of how many of that particular vehicle has been sold. Now I want to sort a list of the particular vehicle based on this parameter.
public class Car {
private int selledUnits;
...
public int getSelledUnits() {
return selledUnits;
}
}
public class Bike {
private int selledUnits;
...
public int getSelledUnits() {
return selledUnits;
}
...
}
Now, suppose I have List<Car> cars
and List<Bike> bikes
and I want to sort these cars based on selledUnits
field.
Currently, I am sorting by creating different comparators for a different class .
For example, for Car, I have created below comparator for sorting
class CarComparator implements Comparator<Car> {
public int compare(Car c1, Car c2) {
return c1.selledUnits - c2.selledUnits;
}
}
Similarly for sorting list of bikes below comparator is used :
class BikeComparator implements Comparator<Bike> {
public int compare(Bike b1, Bike b2) {
return b1.selledUnits - b2.selledUnits;
}
}
My question is instead of creating a separate comparator for each of these classes can I create a generic comparator, since both of them are sorting based on the same fields.
I have tried creating a generic comparator by using reflection and it is working fine but wanted to create the same without using reflection.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…