Java Program to sort an array in descending order


In this program, You will learn how to sort an array in descending order in Java.


Array list in descending order: 60 50 40 30 20 10


Example: How to sort an array in descending order in Java

Run it Now
import java.util.Scanner;

class Main {
    public static void main(String args[]) {

        Scanner s = new Scanner(System.in);
        int a[] = new int[10];
        int n;

        System.out.print("Enter Size of An Array :");
        n = s.nextInt();

        System.out.print("\nEnter Array Elements :");
        for (int i = 0; i < n; i++) {
            a[i] = s.nextInt();
        }

        for (int i = 0; i < n; i++) {
            for (int j = 1 + i; j < n; j++) {
                if (a[i] < a[j]) {
                    int temp = a[i];
                    a[i] = a[j];
                    a[j] = temp;
                }
            }
        }

        System.out.print("\nArray List in Descending Order:");
        for (int i = 0; i < n; i++) {
            System.out.print(" " + a[i]);
        }

    }
}

Output:

Enter Size of An Array :5
Enter Array Elements :10 20 40 30 60
Array List in Descending Order: 60 40 30 20 10