public void sort(Comparator<? super E> c) Parameters:c - the Comparator used to compare list elements. A null value indicates that the elements' natural ordering should be used
上面说明形参是实现Comparator接口的类对象,如果为null,则ArrayList集合的元素就按自然顺序来排序
代码验证如下:
1 import java.util.*; 2 3 public class Demo12{ 4 public static void main(String[] args)throws Exception{ 5 6 List<Integer> list = new ArrayList<>(); 7 list.add(94); 8 list.add(45); 9 list.add(28); 10 list.add(58); 11 12 list.sort(null); 13 //遍历list集合元素 14 for(int i:list){ 15 System.out.print(i + " "); 16 } 17 18 System.out.println(); 19 20 //转换为数组打印出来看看 21 System.out.println(Arrays.toString(list.toArray())); 22 } 23 }