HiveBrain v1.2.0
Get Started
← Back to all entries
snippetjavaCritical

How to sort a List/ArrayList?

Submitted by: @import:stackoverflow-api··
0
Viewed 0 times
listhowarraylistsort

Problem

I have a List of Double objects in Java. I want to sort the ArrayList in descending order.

Input ArrayList:

List testList = new ArrayList();

testList.add(0.5);
testList.add(0.2);
testList.add(0.9);
testList.add(0.1);
testList.add(0.1);
testList.add(0.1);
testList.add(0.54);
testList.add(0.71);
testList.add(0.71);
testList.add(0.71);
testList.add(0.92);
testList.add(0.12);
testList.add(0.65);
testList.add(0.34);
testList.add(0.62);


The out put should be like this

0.92
0.9
0.71
0.71
0.71
0.65
0.62
0.54
0.5
0.34
0.2
0.12
0.1
0.1
0.1

Solution

Collections.sort(testList);
Collections.reverse(testList);


That will do what you want. Remember to import Collections though!

Here is the documentation for Collections.

Code Snippets

Collections.sort(testList);
Collections.reverse(testList);

Context

Stack Overflow Q#16252269, score: 696

Revisions (0)

No revisions yet.