OneBite.Dev - Coding blog in a bite size

sort items in array by desc in java

Code snippet on how to sort items in array by desc in java

Arrays.sort(array, Collections.reverseOrder());

This code will sort items in the array in descending order.

First, we are using an algorithm provided by the Arrays class which implements the Comparator interface and it’s reverseOrder method. This will then be applied to the array that is given as an argument, which will reverse the items in the array.

Next, we must give the array as an argument to the Arrays.sort() method. This method will sort the items in the array according to the Comparator that we provided earlier.

Finally, the array will be sorted in descending order, with the highest value at the beginning of the array, and the lowest value at the end of the array.

java