I’m trying to sort an array in descending order in Java using this code:
for(int i = 0; i < arr.length; i++) {
Comparator comparator = Collections.reverseOrder();
Arrays.sort(arr,comparator);
}
But I get this error:
The method sort(int[]) in the type Arrays is not applicable for the arguments (int[], Comparator)
It’s not clear why you’re trying to loop, but the
Arrays.sortmethods operating on primitive arrays don’t allow a custom comparator to be specified.The simplest approach would be to sort and then reverse the array. I can’t immediately find a
reversemethod which would take anint[]but it would be easy to write your own.