How to sort an ArrayList?

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

    You can do like this:

    List yourList = new ArrayList();
    Collections.sort(yourList, Collections.reverseOrder());
    

    Collection has a default Comparator that can help you with that.

    Also, if you want to use some Java 8 new features, you can do like that:

    List yourList = new ArrayList();
    yourList = yourList.stream().sorted(Collections.reverseOrder()).collect(Collectors.toList());
    

提交回复
热议问题