# Comparator详解

Comparator是一个专用的比较器，当这个对象不支持自比较或者自比较函数不能满足要求时，可写一个比较器来完成两个对象之间大小的比较。Comparator体现了一种策略模式(strategy design pattern)，就是不改变对象自身，而用一个策略对象(strategy object)来改变它的行为。

`````` Integer [] values = new Integer[]{1,0,3,5,7,8,5,4,3,7,9,56,3,23};
Arrays.sort(values, new Comparator<Integer>() {
@Override
public int compare(Integer value1, Integer value2) {
return value1- value2;
}
});
``````

``````Integer [] values = new Integer[]{1,0,3,5,7,8,5,4,3,7,9,56,3,23};
Arrays.sort(values, (x,y) -> x-y);
``````

``````Integer [] values = new Integer[]{1,0,3,5,7,8,5,4,3,7,9,56,3,23};
Arrays.sort(values, Comparator.comparingInt(x -> x));
``````

## Comparator使用

Comparator是一个函数式接口,只有一个 int compare(T o1, T o2);方法供外部使用,但是java8在其基础上定制了许多default方法,这些default方法,开箱即用,下面我们就看看有哪些default方法以及如何使用.

reversed()

``````default Comparator<T> reversed() {
return Collections.reverseOrder(this);
}
``````

``````Integer [] values = new Integer[]{1,0,3,5,7,8,5,4,3,7,9,56,3,23};
Comparator<Integer> comparator = Comparator.comparingInt(x -> x);
Arrays.sort(values, comparator);
System.out.println(JSON.toJSONString(values));
Arrays.sort(values, comparator.reversed());
System.out.println(JSON.toJSONString(values));
``````

``````[0,1,3,3,3,4,5,5,7,7,8,9,23,56]
[56,23,9,8,7,7,5,5,4,3,3,3,1,0]
``````

thenComparing(Comparator<? super T> other)

``````default Comparator<T> thenComparing(Comparator<? super T> other) {
Objects.requireNonNull(other);
return (Comparator<T> & Serializable) (c1, c2) -> {
int res = compare(c1, c2);
return (res != 0) ? res : other.compare(c1, c2);
};
}
``````

person类

``````class Person{
private int age;
private String name;
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Person(int age, String name) {
this.age = age;
this.name = name;
}
@Override
public String toString() {
return "Person{" +
"age=" + age +
", name='" + name + '\'' +
'}';
}
}
``````

``````Person [] persons = new Person[]{
new Person(22,"zhangsan"),
new Person(23,"lisi"),
new Person(23,"wangwu"),
new Person(22,"wangwu"),
new Person(22,"lisi"),
new Person(23,"zhangsan")
};
System.out.println(JSON.toJSONString(persons));
Comparator<Person> comparator = Comparator.comparingInt(x -> x.name.length());
Arrays.sort(persons,comparator);
System.out.println(JSON.toJSONString(persons));
comparator = comparator.thenComparing(Comparator.comparingInt(x->x.age));
Arrays.sort(persons,comparator);
System.out.println(JSON.toJSONString(persons));
``````

``````[{"age":22,"name":"zhangsan"},{"age":23,"name":"lisi"},{"age":23,"name":"wangwu"},{"age":22,"name":"wangwu"},{"age":22,"name":"lisi"},{"age":23,"name":"zhangsan"}]
[{"age":23,"name":"lisi"},{"age":22,"name":"lisi"},{"age":23,"name":"wangwu"},{"age":22,"name":"wangwu"},{"age":22,"name":"zhangsan"},{"age":23,"name":"zhangsan"}]
[{"age":22,"name":"lisi"},{"age":23,"name":"lisi"},{"age":22,"name":"wangwu"},{"age":23,"name":"wangwu"},{"age":22,"name":"zhangsan"},{"age":23,"name":"zhangsan"}]
``````

``````Comparator<Person> comparator = Comparator.comparing(Person::getName).thenComparing(Person::getAge);
``````

default <U> Comparator<T> thenComparing(
Function<? super T, ? extends U> keyExtractor,
Comparator<? super U> keyComparator)

``````default <U> Comparator<T> thenComparing(
Function<? super T, ? extends U> keyExtractor,
Comparator<? super U> keyComparator)
{
return thenComparing(comparing(keyExtractor, keyComparator));
}
``````