Leetcode: Arrays.sort() - comparator

1. 一般可以用简易版: Arrays.sort(lst, (a,b) -> a[0]-b[0]); 

但是在遇到a,b是large numbers时候遇到困难,改成:

Arrays.sort(points,(o1,o2)->{
            if(o1[1] == o2[1]) return 0;
            if(o1[1] < o2[1]) return -1;
            return 1;
        })

2. 根据dp[1]进行升序排列,O(NlogN)

Arrays.sort(dp, (o1,o2)->o1[0]==o2[0]?o1[1]-o2[1]:o1[0]-o2[0]);

 3. Integer不可以直接对比,需要.intValue() == .intValue()

or

Arrays.sort(intervals, (a, b) -> a[0].compareTo(b[0]));
Arrays.sort(intervals, (a, b) -> Integer.compare(a[0], b[0]));

 Integer[]

 Arrays.sort(arr, Collections.reverseOrder());

 

posted @ 2023-06-23 11:47  PEAR2020  阅读(18)  评论(0编辑  收藏  举报