Java program to sort string array in descending order using collections
Program
import java.util.Arrays;
import java.util.Scanner;
import java.util.Collections;
public class SortStringArrayInDecendingOrderUsingCollections{
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();
String inputArray[] = new String[size];
System.out.println("Enter the elements of the array: ");
for (i = 0; i < size; i++) {
inputArray[i] = reader.next();
}
Arrays.sort(inputArray, Collections.reverseOrder());
System.out.println("String Array elements sorted in decending order: ");
System.out.println(Arrays.toString(inputArray));
}
}
Output
javac .\SortStringArrayInDecendingOrderUsingCollections.java
java SortStringArrayInDecendingOrderUsingCollections
Enter the required size of the array:
4
Enter the elements of the array:
java
python
ada
php
String Array elements sorted in decending order:
[python, php, java, ada]