To sort an ArrayList in Java in descending order in following way :-
- Create an ArrayList
- To Sort the data of the ArrayList using the Collections.sort() method of the Collections class
- Now reverse array list using the Collections.reverseOrder() method of the Collections class.
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class SortReverseAL {
public static void main(String[] args) {
List nList = new ArrayList<>();
nList.add(15);
nList.add(50);
nList.add(233);
nList.add(400);
nList.add(250);
Comparator cmp = Collections.reverseOrder();
Collections.sort(nList, cmp);
System.out.println(nList);
}
}
Output :-
[400, 250, 233, 50, 15]