How to Sort ArrayList in Descending Order in Java

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.

How to Sort ArrayList in Descending Order in Java

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]

How to Sort ArrayList in Descending Order in Java.


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.

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 –

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.