how to sort list in descending order in java code example
Example 1: java list sort descending
import java.util.*;
public class Details {
public static void main(String args[]){
ArrayList<String> arraylist = new ArrayList<String>();
arraylist.add("AA");
arraylist.add("ZZ");
arraylist.add("CC");
arraylist.add("FF");
System.out.println("Before Sorting:");
for(String str: arraylist){
System.out.println(str);
}
Collections.sort(arraylist, Collections.reverseOrder());
System.out.println("ArrayList in descending order:");
for(String str: arraylist){
System.out.println(str);
}
}
}
Example 2: sorting array in descending order java
import java.util.Arrays;
import java.util.Collections;
public class SortExample
{
public static void main(String[] args)
{
Integer[] arr = {13, 7, 6, 45, 21, 9, 2, 100};
Arrays.sort(arr, Collections.reverseOrder());
System.out.printf("Modified arr[] : %s",
Arrays.toString(arr));
}
}