How to sort an ArrayList?

后端 未结 20 2408
有刺的猬
有刺的猬 2020-11-22 06:19

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

Input ArrayList is as below:

List testList = new Arr         


        
20条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-11-22 06:39

    if you are using Java SE 8, then this might be of help.

    //create a comparator object using a Lambda expression
    Comparator compareDouble = (d1, d2) -> d1.compareTo(d2);
    
    //Sort the Collection in this case 'testList' in reverse order
    Collections.sort(testList, Collections.reverseOrder(compareDouble));
    
    //print the sorted list using method reference only applicable in SE 8
    testList.forEach(System.out::println);
    

提交回复
热议问题