Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Java Program to Sort the Array Elements in Descending Order

  • Last Updated : 09 Oct, 2021

Sort the given array in descending order, i.e., arrange the elements from largest to smallest. 

Example:

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.

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]

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.

Approaches

There are numerous approaches to sort the given array in descending order in Java. A few of them are listed below.



  • Using Collections.reverseOrder() method
  • Using Sorting and reversing

1. Using Collections.reverseOrder() method

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 log N)

2. Using Sorting and Reversing

  • Sort the given array.
  • 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 ascending 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 log N) 




My Personal Notes arrow_drop_up
Recommended Articles
Page :