Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
4 views
in Java by (10.2k points)

I have a List of doubles in java and I want to sort ArrayList in descending order.

Input ArrayList is as below:

List<Double> 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

1 Answer

0 votes
by (46k points)

Try: 

Collections.sort(testList);

Collections.reverse(testList);

That will perform what you require. Remember to import Collections.

check out this doc.

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
asked Nov 13, 2019 in Java by Nigam (4k points)

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...