How to use Comparator in Java to sort

后端 未结 14 2009
时光取名叫无心
时光取名叫无心 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 02:56

    For the sake of completeness, here's a simple one-liner compare method:

    Collections.sort(people, new Comparator() {
        @Override
        public int compare(Person lhs, Person rhs) {  
            return Integer.signum(lhs.getId() - rhs.getId());  
        }
    });
    

提交回复
热议问题