Complex Comparator
A comparator can use if statements to compare two objects using multiple criteria, e.g. year first, then month, then day.
Comparator<MyDate> cmp = new Comparator<MyDate>() { public int compare(MyDate x, MyDate y) { int ans = x.year - y.year; if (ans != 0) return ans; ans = x.month - y.month; if (ans != 0) return ans; return x.day - y.day; }}; int res = cmp.compare(datea, dateb);
Sometimes it is convenient for the comparator to simply return a value such as 1 or -1 to indicate that one object should be sorted before the other.