🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
# 1.4-排序: Guava强大的”流畅风格比较器” [原文链接](http://code.google.com/p/guava-libraries/wiki/OrderingExplained) 译者: 沈义扬 [排序器[Ordering]](http://docs.guava-libraries.googlecode.com/git-history/release/javadoc/com/google/common/collect/Ordering.html)是Guava流畅风格比较器[Comparator]的实现,它可以用来为构建复杂的比较器,以完成集合排序的功能。 从实现上说,Ordering实例就是一个特殊的Comparator实例。Ordering把很多基于Comparator的静态方法(如Collections.max)包装为自己的实例方法(非静态方法),并且提供了链式调用方法,来定制和增强现有的比较器。 **创建排序器**:常见的排序器可以由下面的静态方法创建 | **方法** | **描述** | |:--- |:--- | | [`natural()`](http://docs.guava-libraries.googlecode.com/git-history/release/javadoc/com/google/common/collect/Ordering.html#natural%28%29) | 对可排序类型做自然排序,如数字按大小,日期按先后排序 | | [`usingToString()`](http://docs.guava-libraries.googlecode.com/git-history/release/javadoc/com/google/common/collect/Ordering.html#usingToString%28%29) | 按对象的字符串形式做字典排序[lexicographical ordering] | | [`from(Comparator)`](http://docs.guava-libraries.googlecode.com/git-history/release/javadoc/com/google/common/collect/Ordering.html#from%28java.util.Comparator%29) | 把给定的Comparator转化为排序器 | 实现自定义的排序器时,除了用上面的from方法,也可以跳过实现Comparator,而直接继承Ordering: ``` Ordering<String> byLengthOrdering = new Ordering<String>() { public int compare(String left, String right) { return Ints.compare(left.length(), right.length()); } }; ``` **链式调用方法**:通过链式调用,可以由给定的排序器衍生出其它排序器 | **方法** | **描述** | |:--- |:--- | | [`reverse()`](http://docs.guava-libraries.googlecode.com/git-history/release/javadoc/com/google/common/collect/Ordering.html#reverse%28%29) | 获取语义相反的排序器 | | [`nullsFirst()`](http://docs.guava-libraries.googlecode.com/git-history/release/javadoc/com/google/common/collect/Ordering.html#nullsFirst%28%29) | 使用当前排序器,但额外把null值排到最前面。 | | [`nullsLast()`](http://docs.guava-libraries.googlecode.com/git-history/release/javadoc/com/google/common/collect/Ordering.html#nullsLast%28%29) | 使用当前排序器,但额外把null值排到最后面。 | | [`compound(Comparator)`](http://docs.guava-libraries.googlecode.com/git-history/release/javadoc/com/google/common/collect/Ordering.html#compound%28java.util.Comparator%29) | 合成另一个比较器,以处理当前排序器中的相等情况。 | | [`lexicographical()`](http://docs.guava-libraries.googlecode.com/git-history/release/javadoc/com/google/common/collect/Ordering.html#lexicographical%28%29) | 基于处理类型T的排序器,返回该类型的可迭代对象Iterable&lt;T&gt;的排序器。 | | [`onResultOf(Function)`](http://docs.guava-libraries.googlecode.com/git-history/release/javadoc/com/…le/common/collect/Ordering.html#onResultOf%28com.google.common.base.Function%29) | 对集合中元素调用Function,再按返回值用当前排序器排序。 | 例如,你需要下面这个类的排序器。 ``` class Foo { @Nullable String sortedBy; int notSortedBy; } ``` 考虑到排序器应该能处理sortedBy为null的情况,我们可以使用下面的链式调用来合成排序器: ``` Ordering<Foo> ordering = Ordering.natural().nullsFirst().onResultOf(new Function<Foo, String>() { public String apply(Foo foo) { return foo.sortedBy; } }); ``` 当阅读链式调用产生的排序器时,应该从后往前读。上面的例子中,排序器首先调用apply方法获取sortedBy值,并把sortedBy为null的元素都放到最前面,然后把剩下的元素按sortedBy进行自然排序。之所以要从后往前读,是因为每次链式调用都是用后面的方法包装了前面的排序器。 _注:用compound方法包装排序器时,就不应遵循从后往前读的原则。为了避免理解上的混乱,请不要把compound写在一长串链式调用的中间,你可以另起一行,在链中最先或最后调用compound。_ 超过一定长度的链式调用,也可能会带来阅读和理解上的难度。我们建议按下面的代码这样,在一个链中最多使用三个方法。此外,你也可以把Function分离成中间对象,让链式调用更简洁紧凑。 ``` Ordering<Foo> ordering = Ordering.natural().nullsFirst().onResultOf(sortKeyFunction) ``` **运用排序器:**Guava的排序器实现有若干操纵集合或元素值的方法 | **方法** | **描述** | **另请参见** | |:--- |:--- |:--- | | [`greatestOf(Iterable iterable, int k)`](http://docs.guava-libraries.googlecode.com/git-history/release/javadoc/com/google/common/collect/Ordering.html#greatestOf%28java.lang.Iterable, int%29) | 获取可迭代对象中最大的k个元素。 | [`leastOf`](http://docs.guava-libraries.googlecode.com/git-history/release/javadoc/com/google/common/collect/Ordering.html#leastOf%28java.lang.Iterable, int%29) | | [`isOrdered(Iterable)`](http://docs.guava-libraries.googlecode.com/git-history/release/javadoc/com/google/common/collect/Ordering.html#isOrdered%28java.lang.Iterable%29) | 判断可迭代对象是否已按排序器排序:允许有排序值相等的元素。 | [`isStrictlyOrdered`](http://docs.guava-libraries.googlecode.com/git-history/release/javadoc/com/google/common/collect/Ordering.html#isStrictlyOrdered%28java.lang.Iterable%29) | | [`sortedCopy(Iterable)`](http://docs.guava-libraries.googlecode.com/git-history/release/javadoc/com/google/common/collect/Ordering.html#sortedCopy%28java.lang.Iterable%29) | 判断可迭代对象是否已严格按排序器排序:不允许排序值相等的元素。 | [`immutableSortedCopy`](http://docs.guava-libraries.googlecode.com/git-history/release/javadoc/com/google/common/collect/Ordering.html#immutableSortedCopy%28java.lang.Iterable%29) | | [`min(E, E)`](http://docs.guava-libraries.googlecode.com/git-history/release/javadoc/com/google/common/collect/Ordering.html#min%28E, E%29) | 返回两个参数中最小的那个。如果相等,则返回第一个参数。 | [`max(E, E)`](http://docs.guava-libraries.googlecode.com/git-history/release/javadoc/com/google/common/collect/Ordering.html#max%28E, E%29) | | [`min(E, E, E, E...)`](http://docs.guava-libraries.googlecode.com/git-history/release/javadoc/com/google/common/collect/Ordering.html#min%28E, E, E, E...%29) | 返回多个参数中最小的那个。如果有超过一个参数都最小,则返回第一个最小的参数。 | [`max(E, E, E, E...)`](http://docs.guava-libraries.googlecode.com/git-history/release/javadoc/com/google/common/collect/Ordering.html#max%28E, E, E, E...%29) | | [`min(Iterable)`](http://docs.guava-libraries.googlecode.com/git-history/release/javadoc/com/google/common/collect/Ordering.html#min%28java.lang.Iterable%29) | 返回迭代器中最小的元素。如果可迭代对象中没有元素,则抛出NoSuchElementException。 | [`max(Iterable)`](http://docs.guava-libraries.googlecode.com/git-history/release/javadoc/com/google/common/collect/Ordering.html#max%28java.lang.Iterable%29), [`min(Iterator)`](http://docs.guava-libraries.googlecode.com/git-history/release/javadoc/com/google/common/collect/Ordering.html#min%28java.util.Iterator%29), [`max(Iterator)`](http://docs.guava-libraries.googlecode.com/git-history/release/javadoc/com/google/common/collect/Ordering.html#max%28java.util.Iterator%29) |