# 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<T>的排序器。 |
| [`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) |
- Google Guava官方教程(中文版)
- 1-基本工具
- 1.1-使用和避免null
- 1.2-前置條件
- 1.3-常見Object方法
- 1.4-排序: Guava強大的”流暢風格比較器”
- 1.5-Throwables:簡化異常和錯誤的傳播與檢查
- 2-集合
- 2.1-不可變集合
- 2.2-新集合類型
- 2.3-強大的集合工具類:java.util.Collections中未包含的集合工具
- 2.4-集合擴展工具類
- 3-緩存
- 4-函數式編程
- 5-并發
- 5.1-google Guava包的ListenableFuture解析
- 5.2-Google-Guava Concurrent包里的Service框架淺析
- 6-字符串處理:分割,連接,填充
- 7-原生類型
- 9-I/O
- 10-散列
- 11-事件總線
- 12-數學運算
- 13-反射