Ordering类提供了一些链式的排序方法,相比JDK自带的排序方法更加简练、方便。
Ordering中有3种静态方法用于创建Ordering排序器:
根据上面的方法创建Ordering后,可以继续调用如下方法创建组合功能的排序器:
创建完Ordering排序器后,即可使用它对集合或元素进行操作
下面介绍常用方法的使用。
以下示例代码中使用到的People类包含name和age两个属性。
1.natural方法
该方法使用自然排序规则生成排序器,如从小到大、日期先后顺序。使用这个方法之前先介绍一下onResultOf 方法,这个方法接收一个Function函数,该函数的返回值可以用于natural方法排序的依据,即根据这个返回值来进行自然排序,示例代码如下:
@Test
public void testNatural() {
List<People> peopleList = new ArrayList<People>() {{
add(new People("A", 33));
add(new People("B", 11));
add(new People("C", 18));
}};
Ordering<People> ordering = Ordering.natural().onResultOf(new Function<People, Comparable>() {
@Override
public Comparable apply(People people) {
return people.getAge();
}
});
for (People p : ordering.sortedCopy(peopleList)) {
System.out.println(MoreObjects.toStringHelper(p)
.add("name", p.getName())
.add("age", p.getAge())
);
}
}
sortedCopy方法会使用创建的排序器排序并生成一个新的List。对于Ordering.natural().onResultOf方法,阅读顺序是从后往前,即根据onResultOf 方法的返回值按照自然规则创建一个Ordering,然后调用sortedCopy方法排序并生成新List。输出结果如下:
People{name=B, age=11}
People{name=C, age=18}
People{name=A, age=33}
2.reverse方法
这个方法使用反向的排序规则来排序,即若使用natural规则创建Ordering后,再接着调用reverse方法,则按照自然规则的反向,从大到小的规则排序,示例代码如下:
@Test
public void testReverse() {
List<People> peopleList = new ArrayList<People>() {{
add(new People("A", 33));
add(new People("B", 11));
add(new People("C", 18));
}};
Ordering<People> ordering = Ordering.natural().reverse().onResultOf(new Function<People, Comparable>() {
@Override
public Comparable apply(People people) {
return people.getAge();
}
});
for (People p : ordering.sortedCopy(peopleList)) {
System.out.println(MoreObjects.toStringHelper(p)
.add("name", p.getName())
.add("age", p.getAge())
);
}
}
输入结果如下:
People{name=A, age=33}
People{name=C, age=18}
People{name=B, age=11}
3.usingToString方法
该方法创建Ordering,并根据排序依据值的toString方法值来使用natural规则排序,示例代码如下:
@Test
public void testUsingToString() {
List<People> peopleList = new ArrayList<People>() {{
add(new People("A", 33));
add(new People("B", 11));
add(new People("C", 18));
}};
Ordering<People> ordering = Ordering.usingToString().onResultOf(new Function<People, Comparable>() {
@Override
public Comparable apply(People people) {
return people.getName();
}
});
for (People p : ordering.sortedCopy(peopleList)) {
System.out.println(MoreObjects.toStringHelper(p)
.add("name", p.getName())
.add("age", p.getAge())
);
}
}
输入结果如下:
People{name=A, age=33}
People{name=B, age=11}
People{name=C, age=18}
同时使用usingToString和reverse方法示例代码如下:
@Test
public void testUsingToStringAndReverse() {
List<People> peopleList = new ArrayList<People>() {{
add(new People("A", 33));
add(new People("B", 11));
add(new People("C", 18));
}};
Ordering<People> ordering = Ordering.usingToString().reverse().onResultOf(new Function<People, Comparable>() {
@Override
public Comparable apply(People people) {
return people.getName();
}
});
for (People p : ordering.sortedCopy(peopleList)) {
System.out.println(MoreObjects.toStringHelper(p)
.add("name", p.getName())
.add("age", p.getAge())
);
}
}
输出结果如下:
People{name=C, age=18}
People{name=B, age=11}
People{name=A, age=33}
4.from方法
该方法接收一个自定义的Comparator比较器来创建Ordering,根据Comparator中的自定义规则排序,示例代码如下:
@Test
public void testFrom() {
List<People> peopleList = new ArrayList<People>() {{
add(new People("A", 33));
add(new People("B", 11));
add(new People("C", 18));
}};
Ordering<People> ordering = Ordering.from(new Comparator<People>() {
@Override
public int compare(People o1, People o2) {
return o1.getAge() - o2.getAge();
}
});
for (People p : ordering.sortedCopy(peopleList)) {
System.out.println(MoreObjects.toStringHelper(p)
.add("name", p.getName())
.add("age", p.getAge())
);
}
}
输出结果如下:
People{name=B, age=11}
People{name=C, age=18}
People{name=A, age=33}
同时使用from和reverse方法,示例代码如下:
@Test
public void testFromAndReverse() {
List<People> peopleList = new ArrayList<People>() {{
add(new People("A", 33));
add(new People("B", 11));
add(new People("C", 18));
}};
Ordering<People> ordering = Ordering.from(new Comparator<People>() {
@Override
public int compare(People o1, People o2) {
return o1.getAge() - o2.getAge();
}
}).reverse();
for (People p : ordering.sortedCopy(peopleList)) {
System.out.println(MoreObjects.toStringHelper(p)
.add("name", p.getName())
.add("age", p.getAge())
);
}
}
输出结果如下:
People{name=A, age=33}
People{name=C, age=18}
People{name=B, age=11}