Given an unsorted ArrayList, the task is to sort this ArrayList in descending order in Java.
Input: Unsorted ArrayList: [Geeks, For, ForGeeks, GeeksForGeeks, A computer portal]
Output: Sorted ArrayList: [GeeksForGeeks, Geeks, ForGeeks, For, A computer portal]
Input: Unsorted ArrayList: [Geeks, For, ForGeeks]
Output: Sorted ArrayList: [Geeks, ForGeeks, For]
Approach: An ArrayList can be Sorted by using the sort() method of the Collections Class in Java. This sort() method takes the collection to be sorted and Collections.reverseOrder() as the parameter and returns a Collection sorted in the Descending Order. Collections.reverseOrder() acts as the comparator in this method.
Below is the implementation of the above approach:
Unsorted ArrayList: [Geeks, For, ForGeeks, GeeksForGeeks, A computer portal] Sorted ArrayList in Descending order : [GeeksForGeeks, Geeks, ForGeeks, For, A computer portal]
- How to sort TreeSet in descending order in Java?
- How to sort an ArrayList in Ascending Order in Java
- Sort first half in ascending and second half in descending order | Set 2
- ArrayList of ArrayList in Java
- Sort an array of string of dates in ascending order
- Sort only non-prime numbers of an array in increasing order
- Java.util.ArrayList.addall() method in Java
- ArrayList in Java
- Arraylist.contains() in Java
- Java.util.ArrayList.add() Method in Java
- Java.util.Arraylist.indexOf() in Java
- Custom ArrayList in Java
- Arraylist lastIndexOf() in Java with example
- ArrayList trimToSize() in Java with example
- ArrayList isEmpty() in Java with example
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.