In Java ArrayList elements are added in sequential order and while iterating an arraylist same sequential order will be used to retrieve the elements. Sometimes you may have a requirement to sort an arraylist in ascending or descending order. In this post we'll see how to sort an ArrayList in descending order in Java.
Sorting ArrayList in descending order
For sorting ArrayList in descending order in Java there are two options
- Use method reverseOrder() provided by Collections class. See example.
Using a custom comparator. See example.
General form and description
Returns a comparator that imposes the reverse of the natural ordering on a collection of objects that implement the Comparable interface.
public static <T> Comparator<T> reverseOrder()
You must also know about the overloaded methodsort provided by the Collections class.
- public static <T> void sort(List<T> list, Comparator<? super T> c) - Sorts the specified list according to the order induced by the specified comparator. All elements in the list must be mutually comparable using the specified comparator (that is, c.compare(e1, e2)must not throw a ClassCastException for any elements e1 and e2 in the list).
Sorting ArrayList Using reverseOrder method
reverseOrder() method mentioned above can be provided as the second parameter in the sort method mentioned above and you will get the ArrayList sorted in reverse order. Let's see an example.
In the program Collections.reverseOrder() method is passed as an argument to the Collections.sort() method to sort ArrayList in reverse order.
public class SortListDemo {
public static void main(String[] args) {
// Using diamond operator (Right side no type specified)
// Available from Java7 onwards
List<String> cityList = new ArrayList<>();
cityList.add("Delhi");
cityList.add("Mumbai");
cityList.add("Bangalore");
cityList.add("Chennai");
cityList.add("Kolkata");
cityList.add("Mumbai");
// sorting the list in descending order
Collections.sort(cityList, Collections.reverseOrder());
//Displaying the list
for(String city : cityList){
System.out.println("Name " + city);
}
}
}
Output
Name Mumbai
Name Mumbai
Name Kolkata
Name Delhi
Name Chennai
Name Bangalore
Sorting Java ArrayList Using custom Comparator
Internally reverseOrder method calls a Comparator class to do the sorting in reverse order. You can do it yourself too by writing your own comparator class. Writing your own Comparator gives you more control over the object ordering.
public class SortListDemo {
public static void main(String[] args) {
// Using diamond operator (Right side no type specified)
// Available from Java7 onwards
List<String> cityList = new ArrayList<>();
cityList.add("Delhi");
cityList.add("Mumbai");
cityList.add("Bangalore");
cityList.add("Chennai");
cityList.add("Kolkata");
cityList.add("Mumbai");
// sorting the list in descending order
Collections.sort(cityList, new MyComparator());
//Displaying the list
for(String city : cityList){
System.out.println("Name " + city);
}
}
}
//Custom comparator class
class MyComparator implements Comparator<String>{
@Override
public int compare(String o1, String o2) {
return o2.compareTo(o1);
}
}
- Refer Java Lambda Expression Comparator Example to see how you can implement a Comparator as a lambda expression.
Recommendations for learning
- Java Programming Masterclass Course
- Java In-Depth: Become a Complete Java Engineer!
- Spring Framework Master Class Course
- Complete Python Bootcamp Course
- Python for Data Science and Machine Learning
That's all for this topic How to Sort an ArrayList in Descending Order in Java. If you have any doubt or any suggestions to make please drop a comment. Thanks!
Related Topics
You may also like-