List数字集合排序

工作中有时候用到List<Integer>排序,记录一下。

直接调用 sortList 方法即可,基于内存的排序,调用后List中的内容就是已经排好序的了

/**
     * 数字集合排序
     * @param list
     */
    public static void sortList(List<Integer> list){
        ComparatorList cl = new ComparatorList();
        Collections.sort(list, cl);
    }

    /**
     * 内部类来自己实现比较器的接口,按照我们自己的排序规则
     *
     */
    static class ComparatorList implements Comparator<Integer>{
        @Override
        public int compare(Integer o1, Integer o2) {
            // 这里返回的值,1升序 -1降序
            return o1>o2 ? 1:-1;
        }

    }

猜你喜欢

转载自blog.csdn.net/zxl646801924/article/details/84648776
今日推荐