Test implements Comparator and override compare() method
public class Test implements Comparator<Test>{
private int priority;
private String desciption;
@Override
public int compare(Test o1, Test o2) {
// your code here
}
}
Answer from Ruchira Gayan Ranaweera on Stack OverflowVideos
Test implements Comparator and override compare() method
public class Test implements Comparator<Test>{
private int priority;
private String desciption;
@Override
public int compare(Test o1, Test o2) {
// your code here
}
}
Comparator<Test> myComparator = new Comparator<Test>() {
public int compare(Test o1, Test o2) {
int result = o2.desciption.compareTo(o1.desciption);
if (result == 0) {
return o1.priority - o2.priority;
}
return result;
}
};
List<Test> sortedList = Collections.sort(testList, myComparator);
First of all, this question seems to be more appropriate for Code Review. But there are some concepts to explain that go beyond code review, so I decided to post an answer.
First draft
Your comparator can be considered as a first draft. It is working well and compares two Date objects as specified. Well done.
Code improvement
The many if-else-statements make the comparator somewhat clumsy and unreadable. Keep in mind that the compare method is not bound to returning -1, 0, or 1. It can return any negative number if the first argument is less than the second one, and any positive number if the first argument is greater than the second one. Only the 0 return is bound to equality.
As month and day are both represented as integers, you can simply use them in a little arithmetic. The month difference is more important - it weighs more - so it must be heavier:
public int compare(Date date1, Date date2) {
int monthDiff = date1.getMonth() - date2.getMonth();
int dayDiff = date1.getDay() - date2.getDay();
return monthDiff * 100 + dayDiff;
}
Subtractions already produce a negative number, a zero, or a positive number. So use them. The factor 100 makes the month difference more important than the day difference.
If the month difference is not 0, then adding the day difference will not have any effect (because of the factor 100). Only when the month difference is 0, then the day difference will be important.
Code structure
Comparing two dates this way looks very natural. In fact, this is a natural ordering on dates. If a type has such a natural ordering, you should (must) let it implement Comparable:
public class Date implements Comparable<Date> {
...
@Override
public int compareTo(Date other) {
int monthDiff = this.getMonth() - other.getMonth();
int dayDiff = this.getDay() - other.getDay();
return monthDiff * 100 + dayDiff;
}
}
Other comparators
If you feel that you must have some other comparators, you can always add them. A good place is a nested static class inside your Date class (as they simply belong to it).
Let's make a comparator that only take the month into account:
public class Date implements Comparable<Date> {
...
public static final class MonthComparator implements Comparator<Date> {
@Override
public int compare(Date date1, Date date2) {
return date1.getMonth() - date2.getMonth();
}
}
}
his is one (almost) correct way to do it. I write almost because any of the two Dates may be null, and you should probably check for that.
Apart from that, it seems good enough.
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());
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()));