Java Program to Sort Array in Descending Order

Write a Java Program to Sort Array in Descending Order without using Built-in function, and using Sort method with an example. 

Java Program to Sort Array in Descending Order

In this program, we are using the Collections.reverseOrder() and Array.sort method to sort the array elements in descending order.

// Java Program to Sort Array in Descending Order
import java.util.Arrays;
import java.util.Collections;
import java.util.Scanner;
import java.util.Comparator;

public class SortDescending1 {
	private static Scanner sc;
	public static void main(String[] args) 
	{
		int Size, i;
		sc = new Scanner(System.in);
	 
		System.out.print(" Please Enter Number of elements in an array : ");
		Size = sc.nextInt();	
		
		Object [] a = new Object[Size];
		
		System.out.print(" Please Enter " + Size + " elements of an Array  : ");
		for (i = 0; i < Size; i++)
		{
			a[i] = sc.nextInt();
		}
		
		Comparator<Object> cms = Collections.reverseOrder();
		Arrays.sort(a, cms);
		
		System.out.println("\n Result of a Sorted Array :  ");
		for (Object Number: a)
		{
			System.out.print(Number + "  ");
		}
	}
}

Java Sort Array in Descending Order output

 Please Enter Number of elements in an array : 10
 Please Enter 10 elements of an Array  : 89 56 42 567 14 189 222 11 7 99

 Result of a Sorted Array :  
567  222  189  99  89  56  42  14  11  7  

Please refer to Array.sort article.

Java Program to Sort Array in Descending Order using Temp Variable

This Java program allows the user to enter the size and Array elements. Next, it will sort the array element in descending order using For Loop.

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

public class SortDescending2 {
	private static Scanner sc;
	public static void main(String[] args) 
	{
		int Size, i, j, Temp;
		sc = new Scanner(System.in);
	 
		System.out.print(" Please Enter Number of elements in an array : ");
		Size = sc.nextInt();	
		
		int [] a = new int[Size];
		
		System.out.print(" Please Enter " + Size + " elements of an Array  : ");
		for (i = 0; i < Size; i++)
		{
			a[i] = sc.nextInt();
		}
		
		for (i = 0; i < Size; i++)
		{
			for (j = i + 1; j < Size; j++)
			{
				if(a[i] < a[j])
				{
					Temp = a[i];
					a[i] = a[j];
					a[j] = Temp;
				}		
			}
		}
		
		System.out.print("\n Result of a Descending Array :  ");
		for (i = 0; i < Size; i++)
		{
			System.out.print(a[i] + " ");
		}
	}
}
Java Program to Sort Array in Descending Order 2

User inserted values in this Java program are a[5] = {24, 19, 65, 5, 99}

First For Loop – First Iteration: for (i = 0; 0 < 5; 0++)
The value of i will be 0, and the condition (i < 5) is True. So, it will enter into inner or Nested For Loop.

Second For Loop – First Iteration: for (j = 0 + 1; 1 < 5; 1++)
The condition (1 < 5) is True. So, it will enter into If Statement.

if(a[i] < a[j]) => if(24 < 19) – Condition is False

Second For Loop – Second Iteration: for (j = 2; 2 < 5; 2++)
The condition (i < 5) is True.

if(a[i] < a[j]) => if(24 < 65) – Condition is True

Temp = a[0]
Temp = 24

a[0] = a[2]
a[0] = 65

a[j] = Temp
a[2] = 24

Do the same for remaining iterations until the condition (i < 5) fails.

The next for loop is to print the elements after sorting the array. I suggest you refer Java Program to Print Array Elements article to understand the same

Program to Sort Array in Descending Order using Functions

This program is the same as above. Still, this time we separated the logic to sort elements of an array in descending order using Method.

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

public class SortDescending3 {
	private static Scanner sc;
	public static void main(String[] args) 
	{
		int Size, i;
		sc = new Scanner(System.in);
	 
		System.out.print(" Please Enter Number of elements in an array : ");
		Size = sc.nextInt();	
		
		int [] a = new int[Size];
		
		System.out.print(" Please Enter " + Size + " elements of an Array  : ");
		for (i = 0; i < Size; i++)
		{
			a[i] = sc.nextInt();
		}
		
		SortDescending(a, Size);
		System.out.print("\n Result of a Descending Array :  ");
		printAttay(a, Size);
	}
	public static void SortDescending(int[] a, int Size)
	{
		int i, j, Temp;
		
		for (i = 0; i < Size; i++)
		{
			for (j = i + 1; j < Size; j++)
			{
				if(a[i] < a[j])
				{
					Temp = a[i];
					a[i] = a[j];
					a[j] = Temp;
				}			
			}
		}	
	}
	public static void printAttay(int[] Array, int Size)
	{
		int i;
		
		for (i = 0; i < Size; i++)
		{
			System.out.print(Array[i] + " ");
		}
	}
}
 Please Enter Number of elements in an array : 8
 Please Enter 8 elements of an Array  : 25 95 86 15 88 77 5 126

 Result of a Descending Array :  126 95 88 86 77 25 15 5