java定制化排序,多个条件排序

方法针对的排序情况:

有一个实体类命名为test,其中共有三个指标需要排序展示,

one优先级》two优先级》three优先级

在指标属性one一样的时候会查询two指标比较


@org.junit.Test
    public void test() {
        List testList = new ArrayList<>();
        Test one = new Test(1300, 20, 12);
        testList.add(one);
        Test two = new Test(200, 10, 22);
        testList.add(two);
        Test Three = new Test(400, 50, 32);
        testList.add(Three);
        Test four = new Test(500, 30, 25);
        testList.add(four);
        Test five = new Test(600, 70, 89);
        testList.add(five);
        Test six = new Test(700, 90, 59);
        testList.add(six);
        Test seven = new Test(800, 20, 75);
        testList.add(seven);
        Test eight = new Test(900, 21, 35);
        testList.add(eight);
        Test nine = new Test(250, 20, 61);
        testList.add(nine);
        Test ten = new Test(900, 21, 89);
        testList.add(ten);
        testList.sort((o1, o2) -> o1.getOne() > o2.getOne()
                        ? 1 : o1.getOne() < o2.getOne()
                        ? -1 : o1.getTwo() > o2.getTwo()
                        ? 1 : o1.getTwo() < o2.getTwo()
                        ? -1 :  o1.getThree() > o2.getThree()
                        ? 1 : o1.getThree() < o2.getThree()
                        ?  -1 :  0);
        for (Test test : testList) {
            System.out.println(test.getOne() + "\t" + test.getTwo() + "\t" + test.getThree());
        }
    }

    class Test {
        private int one ;
        private int two;
        private int three;

        public Test(int one, int two, int three) {
            this.one = one;
            this.two = two;
            this.three = three;
        }

        public int getOne() {
            return one;
        }

        public void setOne(int one) {
            this.one = one;
        }

        public int getTwo() {
            return two;
        }

        public void setTwo(int two) {
            this.two = two;
        }

        public int getThree() {
            return three;
        }

        public void setThree(int three) {
            this.three = three;
        }
    }

排序结果展示

java定制化排序,多个条件排序_第1张图片

 
 
 
 
 
 
 

你可能感兴趣的:(java8)