Task: How to use Java Arrays sort method for Descending order.
How To Use Arrays.sort() method to Sort Arrays in Descending Order?
In a previous post, we wrote a Java program to sort array in ascending order using Arrays.sort(array-name) method. We can use the following syntax to sort in descending order: Arrays.sort(array, Collections.reverseOrder());
Here we need to use: import java.util.Collections; too along with using import java.util.Arrays;
Moreover, Collections.reverseOrder() does not support primitive data types like int, float, double, char etc. Therefore, we will change code for using Integer array instead of using int array.
The Source code of the Java program to sort Integer array in descending order is:
The output of the Java program to sort Integer array in descending order is:
Array in Descending order is : [201, 145, 109, 89, 21, 17, 10, 6, 0, -1]