首先先来看一下Arrays.sort()使用的例子。
例子1:
Arrays.sort(int[] a)
//注意一定要用Integer对象类
Integer[] a1 = {34, 57, 46, 89, 98, 12, 55, 84, 29};
Integer[] a2 = {34, 57, 46, 89, 98, 12, 55, 84, 29};
//增序,Arrays.sort()默认升序
Arrays.sort(a1);
System.out.println("Arrays.sort()升序:");
for (int i = 0; i < a1.length; i++) {
System.out.print(a1[i] + " ");
}
//降序,可用Comparator()匿名内部类
Arrays.sort(a2, new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
return o2.compareTo(o1);
}
});
System.out.println("\nArrays.sort()降序:");
for (int i = 0; i < a2.length; i++) {
System.out.print(a2[i]+ " ");
基础知识点:
若是基本类型ÿ