ArrayList排序

今天遇到ArrayList排序,说起来真是汗,居然不知道,基础是够差,网上查阅一番。发现用到接口Comparator(定义排序规则,即ArrayList里面的对象应该按什么规则排序),可以自己用类实现这个接口,实现compare接口,这个接口返回值即为你的规则。升序降序都可以自己来定,然后OK了,只要用集合类Collections的sort方法就可以了。sort有两个参数,第一个参数是你的比较规则类,第二个则是你的List。方便啊。
恶补基础
======================================================================
sort
public static <T> void sort(List<T> list,
                            Comparator<? super T> 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


======================================================================

你可能感兴趣的:(C++,c,C#,performance)