Sort the ArrayList in descending order

In the previous post, we have seen how to sort the arrayList. In this post, we will see how to sort the arrayList in descending order.

We will use the method Collections.reverseOrder() to sort the arrayList in reverse order.

Syntax:

Collections.sort(arraylist, Collections.reverseOrder());

arraylist: This is the arrayList which needs to be sorted.

Collections.reverseOrder(): This method is used for sorting in reverse order.

Example:

import java.util.ArrayList;
import java.util.Collections;

public class ArrayListEx {

	public static void main(String[] args) {
		ArrayList<Integer> num = new ArrayList<Integer>();
		num.add(4);
		num.add(2);
		num.add(6);
		num.add(1);

		System.out.println("List of integer before sorting : "+num);
		Collections.sort(num, Collections.reverseOrder());  //sort the arraylist in descending order
		
		System.out.println("List of integer after sorting in descending order : "+num);
	}
}

Output:

List of integer before sorting : [4, 2, 6, 1] List of integer after sorting : [6, 4, 2, 1]
Avatar photo

Shekhar Sharma

Shekhar Sharma is founder of testingpool.com. This website is his window to the world. He believes that ,"Knowledge increases by sharing but not by saving".

You may also like...