TUTORIALS

Java program to sort array using reverse order

Written By: Chaitra M
Created at: 28-June-2023 20:36:19
Modified at: 28-June-2023 20:36:39

Program


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

public class SortArrayInReversseOrderUsingCollections {

  public static void main(String[] args) {
    int i = 0;
    System.out.println("Enter the required size of the array: ");
    Scanner reader = new Scanner(System.in);
    int size = reader.nextInt();
    Integer inputArray[] = new Integer[size];
    System.out.println("Enter the elements of the array: ");
    for (i = 0; i < size; i++) {
      inputArray[i] = reader.nextInt();
    }
    Arrays.sort(inputArray, Collections.reverseOrder()); 
    System.out.println("Array elements sorted in decending order: ");
    System.out.println(Arrays.toString(inputArray));
  }
}

Output


javac .\SortArrayInReversseOrderUsingCollections.java
java SortArrayInReversseOrderUsingCollections        
Enter the required size of the array: 
5
Enter the elements of the array: 
10
20
40
60
30
Array elements sorted in decending order: 
[60, 40, 30, 20, 10]

There are no likes. Be the first one to like
Likes: 0
Comments: 0