看看java.util.Collections的api,都是static的,ArrayList实现了List sort(List<T> list) Sorts the specified list into ascending order, according to the natural ordering of its elements.sort(List<T> list, Comparator<? super T> c) Sorts the specified list according to the order induced by the specified comparator.
直接按放入顺序排列
要想排序,如楼上所说,用Collections类
自己编的算法显然不如sort()的这种quickSort算法来的快。
Collections.sort(arraylist, Comparator);
实现一个Comparator来确定排序的规则。
sort(List<T> list)
Sorts the specified list into ascending order, according to the natural ordering of its elements.sort(List<T> list, Comparator<? super T> c)
Sorts the specified list according to the order induced by the specified comparator.
如果你是ArrayList[] arr = new ArrayList[10]数组的话 ,想用Arrays.sort(arr) 是错误的,
原因在于ArrayList本身这个对象没有实现comparable这个接口.
所以 你要根据你自己的业务实现来写个类 去实现 comparator这个接口
然后用Arrays.sort(arr,comparator);来对你的数组去排序 所以以后大家不管是要用Arrays.sort() 还是Collections.sort()
一定要注意你的数组(集合)里面的对象类 要实现comparable这个接口 ,或者去自己实现个comparator类!