Comparator 多重排序

单条件排序

    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);
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 从三月份找实习到现在,面了一些公司,挂了不少,但最终还是拿到小米、百度、阿里、京东、新浪、CVTE、乐视家的研发岗...
    时芥蓝阅读 42,366评论 11 349
  • 1. Java基础部分 基础部分的顺序:基本语法,类相关的语法,内部类的语法,继承相关的语法,异常的语法,线程的语...
    子非鱼_t_阅读 31,765评论 18 399
  • Java SE 基础: 封装、继承、多态 封装: 概念:就是把对象的属性和操作(或服务)结合为一个独立的整体,并尽...
    Jayden_Cao阅读 2,140评论 0 8
  • java笔记第一天 == 和 equals ==比较的比较的是两个变量的值是否相等,对于引用型变量表示的是两个变量...
    jmychou阅读 1,518评论 0 3
  • 在很多次争辩后才知道,放下是唯一能做的。很多问题刨根问底未见得能得到满意的答案。但,放下能不能解决问题?是问题消失...
    我是王凯阅读 324评论 0 4