Java Comparator using .reverseOrder() but with an inner class

后端 未结 4 1795
借酒劲吻你
借酒劲吻你 2021-02-05 03:42

I am creating a simple program to learn about the Java Comparator class. I have sorted an Arraylist into order but now I want to sort the list in descending order b

4条回答
  •  青春惊慌失措
    2021-02-05 04:01

    If you need to use a Comparator which reverses the current order, just return a negative value in the compare method.

    public class ComparatorInverse implements Comparator {
       @Override
       public int compare(Object lhs, Object rhs) {
          return -1;
       }
    }
    
        

    提交回复
    热议问题