I have a list of Cars scheduled for delivery for multiple dates which needs to be sorted on the basis of the below points:
- If
isReady>0,then it should be displayed first in the table. And then the other values come below it for that particular date. - If
isReady>0and Objectgear!=nullthen, it is displayed first in the table for that particular date. Followed by the other values where Objectgear==null. - If
isReady>0, Objectgear!=nulland Objecttyre!=null, then that value is displayed first in the table for that particular date. Followed by the other values where Objectgear==nullandtyre==null.
Here are the class codes:
public class Car {
private int isReady;
private Tyre tyre;
private Gear gear;
private Date deliveryDate;
}
public class Gear {
private int id;
private String type;
}
public class Tyre {
private int id;
private String grip;
}
public class CarComparator implements Comparator<Car> {
@Override
public int compare(Car entry1, Car entry2) {
int value = 0;
if (entry1.getIsReady() > entry2.getIsReady()) {
value = -1;
} else if (entry1.getIsReady() < entry2.getIsReady()) {
value = 1;
} else if (entry1.getIsReady() == entry2.getIsReady()) {
value = 0;
}
return value;
}
}
I have developed a Comparator which works fine for the first condition where
isReady>0. Could you please help me with the other conditions mentioned above.
Thanks in advance.