Collections sort() 合并排序
Collections 的sort方法使用的是合并排序算法,使用时注意list中的元素需要实现Comparable接口。
这里有个简单的例子:
1 public int compareTo(Object obj) {
2 if (obj instanceof TestObject) {
3 TestObject to = (TestObject) obj;
4 if (this.getLength() > to.getLength()) {
5 return 1;
6 } else if (this.getLength() == to.getLength()) {
7 return 0;
8 } else {
9 return -1;
10 }
11 } else {
12 throw new ClassCastException();
13 }
14 }
3 TestObject to = (TestObject) obj;
4 if (this.getLength() > to.getLength()) {
5 return 1;
6 } else if (this.getLength() == to.getLength()) {
7 return 0;
8 } else {
9 return -1;
10 }
11 } else {
12 throw new ClassCastException();
13 }
14 }
下面是源码:
1 /**
2 * Sorts the specified list into ascending order, according to the
3 * <i>natural ordering</i> of its elements. All elements in the list must
4 * implement the <tt>Comparable</tt> interface. Furthermore, all elements
5 * in the list must be <i>mutually comparable</i> (that is,
6 * <tt>e1.compareTo(e2)</tt> must not throw a <tt>ClassCastException</tt>
7 * for any elements <tt>e1</tt> and <tt>e2</tt> in the list).<p>
8 *
9 * This sort is guaranteed to be <i>stable</i>: equal elements will
10 * not be reordered as a result of the sort.<p>
11 *
12 * The specified list must be modifiable, but need not be resizable.<p>
13 *
14 * The sorting algorithm is a modified mergesort (in which the merge is
15 * omitted if the highest element in the low sublist is less than the
16 * lowest element in the high sublist). This algorithm offers guaranteed
17 * n log(n) performance.
18 *
19 * This implementation dumps the specified list into an array, sorts
20 * the array, and iterates over the list resetting each element
21 * from the corresponding position in the array. This avoids the
22 * n<sup>2</sup> log(n) performance that would result from attempting
23 * to sort a linked list in place.
24 *
25 * @param list the list to be sorted.
26 * @throws ClassCastException if the list contains elements that are not
27 * <i>mutually comparable</i> (for example, strings and integers).
28 * @throws UnsupportedOperationException if the specified list's
29 * list-iterator does not support the <tt>set</tt> operation.
30 * @see Comparable
31 */
32 public static <T extends Comparable<? super T>> void sort(List<T> list) {
33 Object[] a = list.toArray();
34 Arrays.sort(a);
35 ListIterator<T> i = list.listIterator();
36 for (int j=0; j<a.length; j++) {
37 i.next();
38 i.set((T)a[j]);
39 }
40 }
2 * Sorts the specified list into ascending order, according to the
3 * <i>natural ordering</i> of its elements. All elements in the list must
4 * implement the <tt>Comparable</tt> interface. Furthermore, all elements
5 * in the list must be <i>mutually comparable</i> (that is,
6 * <tt>e1.compareTo(e2)</tt> must not throw a <tt>ClassCastException</tt>
7 * for any elements <tt>e1</tt> and <tt>e2</tt> in the list).<p>
8 *
9 * This sort is guaranteed to be <i>stable</i>: equal elements will
10 * not be reordered as a result of the sort.<p>
11 *
12 * The specified list must be modifiable, but need not be resizable.<p>
13 *
14 * The sorting algorithm is a modified mergesort (in which the merge is
15 * omitted if the highest element in the low sublist is less than the
16 * lowest element in the high sublist). This algorithm offers guaranteed
17 * n log(n) performance.
18 *
19 * This implementation dumps the specified list into an array, sorts
20 * the array, and iterates over the list resetting each element
21 * from the corresponding position in the array. This avoids the
22 * n<sup>2</sup> log(n) performance that would result from attempting
23 * to sort a linked list in place.
24 *
25 * @param list the list to be sorted.
26 * @throws ClassCastException if the list contains elements that are not
27 * <i>mutually comparable</i> (for example, strings and integers).
28 * @throws UnsupportedOperationException if the specified list's
29 * list-iterator does not support the <tt>set</tt> operation.
30 * @see Comparable
31 */
32 public static <T extends Comparable<? super T>> void sort(List<T> list) {
33 Object[] a = list.toArray();
34 Arrays.sort(a);
35 ListIterator<T> i = list.listIterator();
36 for (int j=0; j<a.length; j++) {
37 i.next();
38 i.set((T)a[j]);
39 }
40 }