Java使用Collections.sort对一个列表进行自定义排序

public static void sort (List<T> list, Comparator<? super T> comparator)
看一下官方的API解释:
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 specified list must be modifiable, but need not be resizable.java

Implementation note: This implementation is a stable, adaptive, iterative mergesort that requires far fewer than n lg(n) comparisons when the input array is partially sorted, while offering the performance of a traditional mergesort when the input array is randomly ordered. If the input array is nearly sorted, the implementation requires approximately n comparisons. Temporary storage requirements vary from a small constant for nearly sorted input arrays to n/2 object references for randomly ordered input arrays.web

The implementation takes equal advantage of ascending and descending order in its input array, and can take advantage of ascending and descending order in different parts of the same input array. It is well-suited to merging two or more sorted arrays: simply concatenate the arrays and sort the resulting array.算法

The implementation was adapted from Tim Peters’s list sort for Python ( TimSort). It uses techiques from Peter McIlroy’s “Optimistic Sorting and Information Theoretic Complexity”, in Proceedings of the Fourth Annual ACM-SIAM Symposium on Discrete Algorithms, pp 467-474, January 1993.api

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.app

根据元素的 天然顺序 对指定列表按升序进行排序。列表中的全部元素都必须实现 Comparable 接口。此外,列表中的全部元素都必须是 可相互比较的(也就是说,对于列表中的任何 e1 和 e2 元素, e1.compareTo(e2) 不得抛出 ClassCastException)。

这个排序是稳定的,即相等的元素的位置不会改变。
这个list必须是能够修改的,可是没必要是size大小可变化。dom

该排序算法是一个通过修改的合并排序算法(其中,若是低子列表中的最高元素小于高子列表中的最低元素,则忽略合并)。此算法提供可保证的 n log(n) 性能。 此实现将指定列表转储到一个数组中,并对数组进行排序,在重置数组中相应位置处每一个元素的列表上进行迭代。这避免了因为试图原地对连接列表进行排序而产生的 n2 log(n) 性能。ide

参数:
list - 要排序的列表。
comparator 比较器svg

看一个例子吧:性能

package com.sort;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class CollectionsTest {

    public static void main(String[] args) {
        List<People> datas = new ArrayList<People>();
        datas.add(new People("Weber", 37));
        datas.add(new People("Jordan", 35));
        datas.add(new People("Johnny", 29));
        System.out.println("before sort");
        for (People item : datas) {
            System.out.println(item.toString());
        }
        Collections.sort(datas, new Comparator<People>() {
            @Override
            public int compare(People o1, People o2) {
                if (o1.age > o2.age) {// 按照年龄大小排序,年龄大则排在后边,返回正数
                    return 1;
                } else {
                    return -1;
                }
            }
        });
        System.out.println("after sort");
        for (People item : datas) {
            System.out.println(item.toString());
        }
    }

    public static class People {
        public String name;
        public int age;

        public People(String name, int age) {
            this.name = name;
            this.age = age;
        }

        @Override
        public String toString() {
            return "People [name=" + name + ", age=" + age + "]";
        }
    }

}

运行结果为:
before sort
People [name=Weber, age=37]
People [name=Jordan, age=35]
People [name=Johnny, age=29]
after sort
People [name=Johnny, age=29]
People [name=Jordan, age=35]
People [name=Weber, age=37]

即按照年龄的大小,从小到大排序。
比较元素大小时,若是返回正数(好比1),则表示排序靠后;
若是返回复数(好比-1),则表示排序靠前。

参考:
http://tool.oschina.net/apidocs/apidoc?api=jdk-zh
http://tool.oschina.net/apidocs/apidoc?api=jdk_7u4