Hurry! Try our new Interactive Courses for FREE. 🥳   🚀

Java Program to Sort an Array in Descending Order

In this tutorial, we will learn how to sort the elements of an array in descending order. Sorting refers to arranging data in order either alphabetically or numerically. But before moving forward, if you are not familiar with the concepts of the array, then do check the article Arrays in Java.

Input: 5 4 6 7 8 2 3 1 9

Output: 9 8 7 6 5 4 3 2 1

Program 1: Sort the Elements of an Array in Descending Order

In this approach, we will see how to use loops to sort an array in descending order. We can sort the array using manual sorting like using for loops. What we can do is use two for loops, one to traverse the array from the starting and another for loop inside the outer one to traverse the next element. In the body, we can compare the adjacent elements and swap if they are not in order. We can also use a temporary variable for the swapping of elements.

Algorithm

  1. Start
  2. Declare an array
  3. Initialize the array
  4. Declare a temporary variable to store the elements while swapping.
  5. Use two for loops for the same.
  6. Use the first for loop to hold the elements and traverse through all the elements.
  7. Use the second for loop to compare with the remaining elements.
  8. Sort the elements by comparing and swapping.
  9. Display the updated array.
  10. Stop

Below is the code for the same.

The below program demonstrates how to sort an array in descending order using loops.

/*Java Program to Sort an Array in Descending Order*/
import java.util.Scanner;

public class Main
{
    public static void main(String[] args)
    {
        Scanner sc=new Scanner(System.in);
        int n;    //Array Size Declaration
        System.out.println("Enter the number of elements :");
        n=sc.nextInt();    //Array Size Initialization
        
        Integer arr[]=new Integer[n];    //Array Declaration
        System.out.println("Enter the elements of the array :");
        for(int i=0;i<n;i++)     //Array Initialization
        {
            arr[i]=sc.nextInt();
        }
        
        int temp = 0;    //Temporary variable to store the element
        
         for (int i = 0; i < arr.length; i++)   //Holds each Array element
         {     
            for (int j = i+1; j < arr.length; j++)    //compares with remaining Array elements
            {     
               if(arr[i] < arr[j]) //Compare and swap
               {    
                   temp = arr[i];    
                   arr[i] = arr[j];    
                   arr[j] = temp;    
               }     
            }     
        }    
          
        System.out.println();    
            
        //Displaying elements of array after sorting    
        System.out.println("Elements of array sorted in descending order: ");    
        for (int i = 0; i < arr.length; i++) 
        {     
            System.out.print(arr[i] + " ");    
        }    
    }
}


Enter the number of elements : 10
Enter the elements of the array : 6 7 4 51 2 3 9 77 8 4

Elements of array sorted in descending order:
77 51 9 8 7 6 4 4 3 2

Program 2: Sort the elements of an Array in Descending Order

In this approach, we will see how to use Arrays.sort() and Collections.reverseOrder() to sort an array in descending order. The Arrays class of ‘java.util’ package provides the sort method that takes an array as an argument and sorts the array. This is a direct sorting method and the array can be sorted in ascending order with just one method call. Then, when Collections.reverseOrder() is used it reverses the order of the array and the resulting array is in descending order now.

Algorithm

  1. Start
  2. Declare an array
  3. Initialize the array
  4. Use the Arrays.sort() to sort the elements in ascending order.
  5. Then, use Collections.reverseOrder () to reverse the order.
  6. The updated array now will be in descending order.
  7. Print the updated array.
  8. Stop

Below is the code for the same.

A point to be noted here is that Collections.reverseOrder() do not work for primitive types. So, we will define an array with Integer.

The below program demonstrates how to sort an array in descending order using Arrays.sort() and Collections.reverseOrder()

/*Java Program to Sort an Array in Descending Order*/

import java.util.Arrays; 
import java.util.Collections; 
import java.util.Scanner;

public class Main 
{ 
    public static void main(String[] args) 
    { 
        //Collections.reverseOrder do not work for primitive Types 
        //define an array with Integer
         Scanner sc=new Scanner(System.in);
         System.out.println("Enter the number of elements ");
         Integer n=sc.nextInt();
         System.out.println("Enter the array elements ");
         Integer[] arr=new Integer[n];
         for(int i=0;i<n;i++)
         {
             arr[i]=sc.nextInt();
         }
 
        //print original array
        System.out.printf("Original Array: %s", 
                 Arrays.toString(arr)); 
 
        // Sorts the array in descending order 
        Arrays.sort(arr, Collections.reverseOrder()); 
 
        //print sorted array  
        System.out.printf("\n\nSorted Array: %s", 
               Arrays.toString(arr)); 
    } 
}


Enter the number of elements 10
Enter the array elements 6 7 4 51 2 3 9 77 8 4
Original Array: [6, 7, 4, 51, 2, 3, 9, 77, 8, 4]

Sorted Array: [77, 51, 9, 8, 7, 6, 4, 4, 3, 2]