How to use Comparator in Java to sort

后端 未结 14 1800
时光取名叫无心
时光取名叫无心 2020-11-22 02:19

I learned how to use the comparable but I\'m having difficulty with the Comparator. I am having a error in my code:

Exception in thread \"main\" java.lang.C         


        
14条回答
  •  既然无缘
    2020-11-22 03:09

    For the sake of completeness.

    Using Java8

    people.sort(Comparator.comparingInt(People::getId));
    

    if you want in descending order

    people.sort(Comparator.comparingInt(People::getId).reversed());
    

提交回复
热议问题