java比较器

对坐标系中点的集合points,按照距离原点的距离进行排序。

Arrays.sort(points, new Comparator<int[]>() {
            @Override
            public int compare(int[] arg0, int[] arg1) {
                return (int) (Math.pow(arg0[0], 2)+ Math.pow(arg0[1], 2)- Math.pow(arg1[0], 2)- Math.pow(arg1[1], 2));
            }
        });

ladam表达式,同样可以达到效果

      Arrays.sort(points,  Comparator.comparingInt(arr -> arr[0]*arr[0]+arr[1]*arr[1]) );

 

posted @ 2020-11-09 09:40  白清欢  阅读(127)  评论(0编辑  收藏  举报