VOOZH about

URL: https://www.geeksforgeeks.org/java/java-program-to-sort-the-array-elements-in-descending-order/

⇱ Java Program to Sort the Elements of an Array in Descending Order - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Java Program to Sort the Elements of an Array in Descending Order

Last Updated : 23 Jul, 2025

Here, we will sort the array in descending order to arrange elements from largest to smallest. The simple solution is to use Collections.reverseOrder() method. Another way is sorting in ascending order and reversing.

1. Using Collections.reverseOrder()

In this example, we will use Collections.reverseOrder() method along with the Arrays.sort() method to sort an array elements in descending order. This method requires the array to be of type Integer instead of int (primitive type).


Output
[5, 4, 3, 2, 1]

Note: When sorting in descending order, Arrays.sort() does not accept an array of the primitive data type.

2. Using Sorting and Reversing

In this approach, first we sort the array in ascending order using Arrays.sort() method and then reverse the array to get the elements in descending order. This method works for primitive types like int.


Output
[5, 4, 3, 2, 1]

Explanation: Here, we get the array elements in descending order for primitive arrays like int[], which is not possible with Collections.reverseOrder(), because this approach only works with arrays of non-primitive types.

Comment