美文网首页
Comparator 多重排序

Comparator 多重排序

作者: FrankDaddy | 来源:发表于2017-05-08 14:23 被阅读474次

单条件排序

    static class PersonComparator implements Comparator<Person> {
        @Override
        public int compare(Person o1, Person o2) {
            return o1.getAge() - o2.getAge();
        }
    }

    // 调用
    Collections.sort(list, new PersonComparator());

多条件排序

    static class PersonComparatorDouble implements Comparator<Person> {
        @Override
        public int compare(Person o1, Person o2) {
            int flag = o1.getAge() - o2.getAge();
            if (flag == 0) {
                return o2.getPrice() - o1.getPrice();
            } else {
                return flag;
            }
        }
    }
    // 调用 age正序 price反序
    Collections.sort(list, new PersonComparatorDouble());

中文,多条件

    static class PersonComparatorThird implements Comparator<Person> {
        @Override
        public int compare(Person o1, Person o2) {
            Comparator cmp = (Collator.getInstance(Locale.CHINA));
            int flag = cmp.compare(o1.getName(), o2.getName());
            if (flag == 0) {
                int flag2 = o1.getAge() - o2.getAge();
                if (flag2 == 0) {
                    return o2.getPrice() - o1.getPrice();
                } else {
                    return flag2;
                }
            } else {
                return flag;
            }
        }
    }
    // 调用 中文排序  name正序  age正序 price反序
    Collections.sort(list, new PersonComparatorThird());

上面用到的实体类

public class Person {
    private String name;
    private int age;
    private int price;
    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }
    public Person(String name, int age, int price) {
        this.name = name;
        this.age = age;
        this.price = price;
    }
    @Override
    public String toString() {
        return "Person[name = " + name + ",age = " + age + " price = " + price + "]";
    }
        // get()... set()...
}

初始化数据:

    private void initData() {
        list = new ArrayList<Person>();
        list.add(new Person("zhang", 22, 9));
        list.add(new Person("li", 80, 8));
        list.add(new Person("huang", 31, 7));
        list.add(new Person("bai", 8, 6));
        list.add(new Person("wei", 57, 5));
        list.add(new Person("huang", 31, 9));
    }

以上是Comparator的用法,如果实现了Comparable<Object>接口
重写compareTo方法也可以直接调用快速对比,不过基本没见人用过

public class Person implements Comparable<Person> {
    private String name;
    private int age;
    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }
    @Override
    public String toString() {
        return "Person[name = " + name + ",age = " + age + "]";
    }
    @Override
    public int compareTo(Person person) {
        return this.getAge() - person.getAge();
    }
}
    // 调用
    Collections.sort(list_imp);

相关文章

网友评论

      本文标题:Comparator 多重排序

      本文链接:https://www.haomeiwen.com/subject/shhmtxtx.html