In this post, we will see how to sort ArrayList in descending order in Java. We will see how to sort the list in descending order using Java 8 stream API and how to sort the list which contains custom objects (For example suppose we have a class called Employee which contains two field name and id and we want to sort on basis of name or id in descending order ). We can sort ArrayList in ascending using Collections.sort()
method. If we want to sort ArrayList in descending order we need to use Collections.sort(list)
and Collections.reverse(list)
together or we can use another overloaded version of sort method i.e Collections.sort(list, Collections.reverseOrder())
. Let’s see examples.
Sorting ArrayList in descending order in Java Examples.
Using Collections.sort(list) and then Collections.reverse(list) method.
Note – Collections.sort() method internally uses merge sort.
package sorting; import java.util.ArrayList; import java.util.Collections; import java.util.List; public class SortingListAscendingOrder { public static void main(String[] args) { List<String> list = new ArrayList<String>(); list.add("ram"); list.add("mohan"); list.add("sohan"); list.add("shyam"); list.add("john"); Collections.sort(list); Collections.reverse(list); for (String s : list) { System.out.println("Sorted in desceding order:- " + s); } } }
Sorting ArrayList in descending order in Java
Output is –
Sorted in Descending order:- [sohan, shyam, ram, mohan, john]
Using Collections.sort(list, Collections.reverseOrder()) method.
Note – Collections.sort() method have two overloaded version. The first one accepts one parameter i.e List only. The second one accepts two parameters List and Comparator.
package sorting; import java.util.ArrayList; import java.util.Collections; import java.util.List; public class SortingListAscendingOrder { public static void main(String[] args) { List<String> list = new ArrayList<String>(); list.add("ram"); list.add("mohan"); list.add("sohan"); list.add("shyam"); list.add("john"); Collections.sort(list, Collections.reverseOrder()); System.out.println("Sorted in Descending order:- " + list); } }
Output is –
Sorted in Descending order:- [sohan, shyam, ram, mohan, john]
Using Java 8 stream method.
package sorting; import java.util.ArrayList; import java.util.List; public class SortingListAscendingOrder { public static void main(String[] args) { List<String> list = new ArrayList<String>(); list.add("ram"); list.add("mohan"); list.add("sohan"); list.add("shyam"); list.add("john"); list.stream().sorted((i1, i2) -> i2.compareTo(i1)).forEach(System.out::println); } }
Output is –
sohan
shyam
ram
mohan
john
We have seen basic about how to Sort ArrayList in Descending Order in Java. Let’s see some more example.
We might want to sort a custom object(for example Employee class object) on the basis of some field. Let’s say we have a class called Employee which contains two fields name and id and we want to sort on basis of name or id in descending order. Let’s see an example using Java 8 lambda expression
package sorting; import java.util.ArrayList; import java.util.List; class Employee { private String name; private int id; public Employee() { } public Employee(String name, int id) { super(); this.name = name; this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getId() { return id; } public void setId(int id) { this.id = id; } } public class SortingListAscendingOrder { public static void main(String[] args) { List<Employee> emplist = new ArrayList<>(); emplist.add(new Employee("ram", 5)); emplist.add(new Employee("mohan", 1)); emplist.add(new Employee("sohan", 3)); emplist.add(new Employee("shyam", 2)); emplist.add(new Employee("john", 6)); System.out.println("sort on basis of name"); emplist.sort((Employee o1, Employee o2) -> o2.getName().compareTo(o1.getName())); emplist.forEach((emp) -> System.out.println(emp.getName())); System.out.println("sort on basis of id"); emplist.sort((Employee o1, Employee o2) -> o2.getId() - o1.getId()); emplist.forEach((emp) -> System.out.println(emp.getId())); } }
Output is –
sort on basis of name
sohan
shyam
ram
mohan
john
sort on basis of id
6
5
3
2
1
Note – For sorting in ascending order we need to modify logic as below. See an example here.
emplist.sort((Employee o1, Employee o2) -> o1.getName().compareTo(o2.getName()))
emplist.sort((Employee o1, Employee o2) -> o1.getId() - o2.getId())
That’s all about How to Sort ArrayList in Descending Order in Java.
You may like –
- Sorting Using Comparator.
- Sorting using Comparable in java.
- Sort HashMap by value in java.
- Sort HashMap by key in java.
- Sorting in Hibernate.
- Sorting in Spring Data JPA using Spring Boot.
- @OrderBy Annotation in Hibernate for Sorting.
- How to sort using Criteria in Hibernate.
- Sorting in Hibernate using HQL.
- Difference between Iterator and ListIterator in Java.
- Difference between Iteration and Enumeration in java.
- Difference between HashSet and HashMap in Java.
- Difference between HashSet and TreeSet in java.
- Difference between List and Set in Java.
- Difference between ArrayList and LinkedList in java.
- ArrayList vs Vector in java.
- How to make List, Set and Map Read Only in Java.
- How to avoid duplicate elements in ArrayList.
- Adding Custom type class object in ArrayList.
- How to Synchronize ArrayList in Java.
- Difference between Arrays and Collections.
- Thread sleep() method in Java
- Thread join() method in Java
- Thread yield() method in Java with Example
- Thread getId() method in Java
- Thread getName() method in java
- Thread dumpStack() method in Java
- Thread currentThread() method in Java
- Thread checkAccess() method in Java
- Thread activeCount() method in Java
- Thread life cycle in Java
- Doubly Circular Linked List in Java
- Fail fast and fail safe example in java.
See docs.
Let’s see some more details about how to Sort ArrayList in Descending Order in Java.
The collection.sort() method internally uses the sort method. There is a method define mergeSort() inside Collections.sort() method. Although internally it only uses Arrays.sort() method which takes two parameters. This So we have seen main points about Sorting ArrayList in descending order in Java.
In this way, we can see how sorting is useful. The same way we can do it for Vector and LinkedList. Although Set provides its own class to sort for example TreeSet. In the case of TreeSet, we no need to use these method, It will take care of sorting. Again the question comes how we will sort in descending using TreeSet.
These are the main points How to Sort ArrayList in Descending Order in Java.