Collections.sort方法底层就是调用的Arrays.sort方法。
写一个例子看源码:
public static void main(String[] args) {
List<String> strings = Arrays.asList("6", "1", "3", "1","2");
Collections.sort(strings);//sort方法在这里
for (String string : strings) {
System.out.println(string);
}
}
跟踪代码,发现Collections.sort调用的是list.sort方法:
@SuppressWarnings("unchecked")
public static <T extends Comparable<? super T>> void sort(List<T> list) {
list.sort(null);
}
继续跟踪,发现是用Arrays.sort:
@SuppressWarnings({"unchecked", "rawtypes"})
default void sort(Comparator<? super E> c) {
Object[] a = this.toArray();
Arrays.sort(a, (Comparator) c);
ListIterator<E> i = this.listIterator();
for (Object e : a) {