使用如下:
List<String> sortedList = new ArrayList<String>();
Collections.sort(sortedList, new java.util.Comparator<String>() {
public int compare(String object1, String object2) {
return object2.compareTo(object1);
}
});
这个例子中,对sortedList将会按照降序排序。
若return object2.compareTo(object1);则为升序排序,由于sort默认采用升序排序,因此不需要多此一举,但是如果需要进行复杂的比较,则需要自己实现compare方法,可以参考这里,另外也可以参考这里。下面是设计到的别的知识:
1、int java.lang.String.compareTo(String anotherString)
- Compares two strings lexicographically. The comparison is based on the Unicode value of each character in the strings. The character sequence represented by this String object is compared lexicographically to the character sequence represented by the argument string. The result is a negative integer if this String object lexicographically precedes the argument string. The result is a positive integer if this String object lexicographically follows the argument string. The result is zero if the strings are equal; compareTo returns 0 exactly when the equals(Object) method would return true.
- This is the definition of lexicographic ordering. If two strings are different, then either they have different characters at some index that is a valid index for both strings, or their lengths are different, or both. If they have different characters at one or more index positions, let k be the smallest such index; then the string whose character at position k has the smaller value, as determined by using the < operator, lexicographically precedes the other string. In this case, compareTo returns the difference of the two character values at position k in the two string -- that is, the value:
- this.charAt(k)-anotherString.charAt(k)
- If there is no index position at which they differ, then the shorter string lexicographically precedes the longer string. In this case, compareTo returns the difference of the lengths of the strings -- that is, the value:
- this.length()-anotherString.length()
- Specified by: compareTo(...) in Comparable
- Parameters:
- anotherString the String to be compared.
- Returns:
- the value 0 if the argument string is equal to this string; a value less than 0 if this string is lexicographically less than the string argument; and a value greater than 0 if this string is lexicographically greater than the string argument.
2、<String> void java.util.Collections.sort(List<String> list, Comparator<? super String> c)
- Sorts the specified list according to the order induced by the specified comparator. All elements in the list must be mutually comparable using the specified comparator (that is, c.compare(e1, e2) must not throw a ClassCastException for any elements e1 and e2 in the list).
- This sort is guaranteed to be stable: equal elements will not be reordered as a result of the sort.
- The sorting algorithm is a modified mergesort (in which the merge is omitted if the highest element in the low sublist is less than the lowest element in the high sublist). This algorithm offers guaranteed n log(n) performance. The specified list must be modifiable, but need not be resizable. This implementation dumps the specified list into an array, sorts the array, and iterates over the list resetting each element from the corresponding position in the array. This avoids the n2 log(n) performance that would result from attempting to sort a linked list in place.
- Parameters:
- list the list to be sorted.
- c the comparator to determine the order of the list. A null value indicates that the elements' natural ordering should be used.
- Throws:
- ClassCastException - if the list contains elements that are not mutually comparable using the specified comparator.
- UnsupportedOperationException - if the specified list's list-iterator does not support the set operation.
- See Also:
- Comparator
3、java.util.Comparator<String>
- A comparison function, which imposes a total ordering on some collection of objects. Comparators can be passed to a sort method (such as Collections.sort or Arrays.sort) to allow precise control over the sort order. Comparators can also be used to control the order of certain data structures (such as sorted sets or sorted maps), or to provide an ordering for collections of objects that don't have a natural ordering.
- The ordering imposed by a comparator c on a set of elements S is said to be consistent with equals if and only if c.compare(e1, e2)==0 has the same boolean value as e1.equals(e2) for every e1 and e2 in S.
- Caution should be exercised when using a comparator capable of imposing an ordering inconsistent with equals to order a sorted set (or sorted map). Suppose a sorted set (or sorted map) with an explicit comparator c is used with elements (or keys) drawn from a set S. If the ordering imposed by c on S is inconsistent with equals, the sorted set (or sorted map) will behave "strangely." In particular the sorted set (or sorted map) will violate the general contract for set (or map), which is defined in terms of equals.
- For example, suppose one adds two elements a and b such that (a.equals(b) && c.compare(a, b) != 0) to an empty TreeSet with comparator c. The second add operation will return true (and the size of the tree set will increase) because a and b are not equivalent from the tree set's perspective, even though this is contrary to the specification of the Set.add method.
- Note: It is generally a good idea for comparators to also implement java.io.Serializable, as they may be used as ordering methods in serializable data structures (like TreeSet, TreeMap). In order for the data structure to serialize successfully, the comparator (if provided) must implement Serializable.
- For the mathematically inclined, the relation that defines the imposed ordering that a given comparator c imposes on a given set of objects S is:
- {(x, y) such that c.compare(x, y) <= 0}.
- The quotient for this total order is:
- {(x, y) such that c.compare(x, y) == 0}.
- It follows immediately from the contract for compare that the quotient is an equivalence relation
- on S, and that the imposed ordering is a total order on S
- When we say that the ordering imposed by c on S is consistent with equals
- , we mean that the quotient for the ordering is the equivalence relation defined by the objects' equals(Object) method(s):
- {(x, y) such that x.equals(y)}.
- This interface is a member of the Java Collections Framework.
- Parameters:
- <T> the type of objects that may be compared by this comparator