Guava Ordering類

Ordering(排序)可以被看作是一個豐富的比較具有增強功能的連結，多個實用方法，多型別排序功能等。

類宣告

```@GwtCompatible
public abstract class Ordering<T>
extends Object
implements Comparator<T>```

類方法

S.N. 方法及說明
1 static Ordering<Object> allEqual()

2 static Ordering<Object> arbitrary()

3 int binarySearch(List<? extends T> sortedList, T key)

4 abstract int compare(T left, T right)

5 <U extends T> Ordering<U> compound(Comparator<? super U> secondaryComparator)

6 static <T> Ordering<T> compound(Iterable<? extends Comparator<? super T>> comparators)

7 static <T> Ordering<T> explicit(List<T> valuesInOrder)

8 static <T> Ordering<T> explicit(T leastValue, T... remainingValuesInOrder)

9 static <T> Ordering<T> from(Comparator<T> comparator)

10 <E extends T> List<E> greatestOf(Iterable<E> iterable, int k)

11 <E extends T> List<E> greatestOf(Iterator<E> iterator, int k)

12 <E extends T> ImmutableList<E> immutableSortedCopy(Iterable<E> elements)

13 boolean isOrdered(Iterable<? extends T> iterable)

14 boolean isStrictlyOrdered(Iterable<? extends T> iterable)

15 <E extends T> List<E> leastOf(Iterable<E> iterable, int k)

16 <E extends T> List<E> leastOf(Iterator<E> elements, int k)

17 <S extends T> Ordering<Iterable<S>> lexicographical()

18 <E extends T> E max(E a, E b)

19 <E extends T> E max(E a, E b, E c, E... rest)

20 <E extends T> E max(Iterable<E> iterable)

21 <E extends T> E max(Iterator<E> iterator)

22 <E extends T> E min(E a, E b)

23 <E extends T> E min(E a, E b, E c, E... rest)

24 <E extends T> E min(Iterable<E> iterable)

25 <E extends T> E min(Iterator<E> iterator)

26 static <C extends Comparable> Ordering<C> natural()

27 <S extends T> Ordering<S> nullsFirst()

28 <S extends T> Ordering<S> nullsLast()

29 <F> Ordering<F> onResultOf(Function<F,? extends T> function)

30 <S extends T> Ordering<S> reverse()

31 <E extends T> List<E> sortedCopy(Iterable<E> elements)

32 static Ordering<Object> usingToString()

方法繼承

• java.lang.Object

Ordering 範例

GuavaTester.java
```import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class GuavaTester {
public static void main(String args[]){
List<Integer> numbers = new ArrayList<Integer>();

Ordering ordering = Ordering.natural();
System.out.println("Input List: ");
System.out.println(numbers);

Collections.sort(numbers,ordering );
System.out.println("Sorted List: ");
System.out.println(numbers);

System.out.println("======================");
System.out.println("List is sorted: " + ordering.isOrdered(numbers));
System.out.println("Minimum: " + ordering.min(numbers));
System.out.println("Maximum: " + ordering.max(numbers));

Collections.sort(numbers,ordering.reverse());
System.out.println("Reverse: " + numbers);

System.out.println("Null added to Sorted List: ");
System.out.println(numbers);

Collections.sort(numbers,ordering.nullsFirst());
System.out.println("Null first Sorted List: ");
System.out.println(numbers);
System.out.println("======================");

List<String> names = new ArrayList<String>();

System.out.println("Another List: ");
System.out.println(names);

Collections.sort(names,ordering.nullsFirst().reverse());
System.out.println("Null first then reverse sorted list: ");
System.out.println(names);
}
}```

驗證結果

`C:\Guava>javac GuavaTester.java`

`C:\Guava>java GuavaTester`

```Input List:
[5, 2, 15, 51, 53, 35, 45, 32, 43, 16]
Sorted List:
[2, 5, 15, 16, 32, 35, 43, 45, 51, 53]
======================
List is sorted: true
Minimum: 2
Maximum: 53
Reverse: [53, 51, 45, 43, 35, 32, 16, 15, 5, 2]