Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to sort ArrayList<Long> in decreasing order?

Tags:

java

How to sort an ArrayList<Long> in Java in decreasing order?

like image 583
Tamara Avatar asked May 05 '11 08:05

Tamara


People also ask

How do you arrange a list in descending order in Java?

Create an ArrayList. Sort the contents of the ArrayList using the sort() method of the Collections class. Then, reverse array list using the reverse() method of the Collections class.

How do you sort a list in descending order?

Sort in Descending order The sort() method accepts a reverse parameter as an optional argument. Setting reverse = True sorts the list in the descending order. Alternatively for sorted() , you can use the following code.

How do you sort a long value in Java?

To sort long array in Java, use the Arrays. sort() method. Let's say the following is our long array. long[] arr = new long[] { 987, 76, 5646, 96,8768, 8767 };


Video Answer


1 Answers

Here's one way for your list:

list.sort(null); Collections.reverse(list); 

Or you could implement your own Comparator to sort on and eliminate the reverse step:

list.sort((o1, o2) -> o2.compareTo(o1)); 

Or even more simply use Collections.reverseOrder() since you're only reversing:

list.sort(Collections.reverseOrder()); 
like image 93
WhiteFang34 Avatar answered Nov 15 '22 23:11

WhiteFang34