Related Articles
Java Program to Sort the Elements of an Array in Descending Order
• Last Updated : 20 Nov, 2020

Sort the given array in descending order i.e arrange the elements from largest to smallest. Sorting is a process of arranging items systematically. sort() is an inbuilt function from java.util.Arrays which is used to sort an array of elements in optimized complexity.

Example:

```Input :Array = {2, 6, 23, 98, 24, 35, 78}
Output:[98, 78, 35, 24, 23, 6, 2]

Input :Array = {1, 2, 3, 4, 5}
Output:[5, 4, 3, 2, 1]

```

Approach #1:

Array elements can be sorted in descending order by passing in the array and Collections.reverseOrder() as parameters to Arrays.sort().

Note: One thing to keep in mind is that when sorting in descending order, Arrays.sort() does not accept an array of the primitive data type.

Implementation:

## Java

 `// Java program to sort the elements in descending order``import` `java.util.*;`` ` `class` `GFG {``    ``public` `static` `void` `main(String[] args)``    ``{`` ` `        ``// Initializing the array``        ``Integer array[] = { ``1``, ``2``, ``3``, ``4``, ``5` `};`` ` `        ``// Sorting the array in descending order``        ``Arrays.sort(array, Collections.reverseOrder());`` ` `        ``// Printing the elements``        ``System.out.println(Arrays.toString(array));``    ``}``}`
Output
```[5, 4, 3, 2, 1]
```

Time Complexity:O(N logN)

Approach #2:

1. Sort the given array.
2. Reverse the sorted array.

Below is the implementation of the above approach:

## Java

 `// Java program to sort the elements in descending order``import` `java.util.*;`` ` `class` `GFG {``    ``public` `static` `void` `main(String[] args)``    ``{`` ` `        ``// Initializing the array``        ``int` `array[] = { ``1``, ``2``, ``3``, ``4``, ``5``, ``6` `};`` ` `        ``// Sorting the array in asscending order``        ``Arrays.sort(array);`` ` `        ``// Reversing the array``        ``reverse(array);`` ` `        ``// Printing the elements``        ``System.out.println(Arrays.toString(array));``    ``}`` ` `    ``public` `static` `void` `reverse(``int``[] array)``    ``{`` ` `        ``// Length of the array``        ``int` `n = array.length;`` ` `        ``// Swaping the first half elements with last half``        ``// elements``        ``for` `(``int` `i = ``0``; i < n / ``2``; i++) {`` ` `            ``// Storing the first half elements temporarily``            ``int` `temp = array[i];`` ` `            ``// Assigning the first half to the last half``            ``array[i] = array[n - i - ``1``];`` ` `            ``// Assigning the last half to the first half``            ``array[n - i - ``1``] = temp;``        ``}``    ``}``}`
Output
```[6, 5, 4, 3, 2, 1]
```

Time Complexity: O(N logN)

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up