Notice: Function _load_textdomain_just_in_time was called incorrectly. Translation loading for the responsive-lightbox domain was triggered too early. This is usually an indicator for some code in the plugin or theme running too early. Translations should be loaded at the init action or later. Please see Debugging in WordPress for more information. (This message was added in version 6.7.0.) in /var/www/wp-includes/functions.php on line 6114

Notice: Function _load_textdomain_just_in_time was called incorrectly. Translation loading for the hueman domain was triggered too early. This is usually an indicator for some code in the plugin or theme running too early. Translations should be loaded at the init action or later. Please see Debugging in WordPress for more information. (This message was added in version 6.7.0.) in /var/www/wp-includes/functions.php on line 6114
Sort the ArrayList in descending order - Testingpool

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...