Sorting an Array in Descending (Reverse) Order

The Arrays class can be used to sort an array of either reference types or primitive types, but the Arrays class does have an overloaded sort method that can sort an array in any way we choose. That overloaded variant of the sort method can only take reference types, and it has an additional parameter which is of type Comparator. This is the parameter that tells how we want the array sorted. In the example below we call the static method reverseOrder on the Collections class. It returns a Comparator instance which will sort the array descending instead of normal ascending.
Since this overloaded sort method only take reference types, we have to use the proper wrapper class (in this case Integer) if we want to work with primitive types.

The output:

Search for more Java info on this site here:
Custom Search

Please type any questions here.