How to sort an ArrayList?

后端 未结 20 2105
有刺的猬
有刺的猬 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:45

    With Java8 there is a default sort method on the List interface that will allow you to sort the collection if you provide a Comparator. You can easily sort the example in the question as follows:

    testList.sort((a, b) -> Double.compare(b, a));
    

    Note: the args in the lambda are swapped when passed in to Double.compare to ensure the sort is descending

提交回复
热议问题