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.

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.

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.

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.

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

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

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

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

看一个例子吧:

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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值