I have learned how to create my own Comparator
, for example, create a simple one to compare based on absolute value
class absComparator implements Comparator<Integer> {
public int compare(Integer a, Integer b) {
return a * a - b * b;
}
}
And of course, this can be used for a customized sort:
List<Integer> list_1 = new ArrayList<>(Arrays.asList(-3, -2, -1, 0, 1, 2));
list_1.sort(new absComparator());
>>> [0, -1, 1, -2, 2, -3]
So this is all good, but what if I want to just compare two Integers based on this comparator to give a boolean value?
// Currently:
System.out.println(-2>1);
>>> false
So how do I get a true
by comparing -2
and 1
, using absComparator
?