What exactly does the following overload of Collections.sort()
:
sort(List<T> list, Comparator<? super T> c)
ask for as the second argument?
Example: I initiate a list of 7 random integers as:
List<Integer> listOfInts = new ArrayList<>();
Random rand = new Random(System.currentTimeMillis());
for (int i = 0; i < 7; i++) {
listOfInts.add(rand.nextInt());
}
Then I try to sort them using Collections.sort()
as:
Collections.sort(listOfInts, Integer :: compare);
as well as:
Collections.sort(listOfInts, Integer :: compareTo);
and they both work. Why doesn't the call to sort the collection using compareTo()
throw/fail? The signature of compareTo()
is totally different from the signature of compare()
.