从小到大排序
public class aa {
public static void main(String[] args) {
int[] a = {
7932191, // 1
11343015, // 2
1844114, // 3
15206720, // 4
7674351, // 5
5390669, // 6
5420431, // 7
9028256, // 8
15752490, // 9
13039082 // 10
};
Arrays.sort(a);
for(int i=0;i<a.length;i++)
System.out.print(a[i]+" "); //从小到大 依次排序
}
}
从大到小排序
public class aa {
public static void main(String[] args) {
Integer[] a = {
7932191, // 1
11343015, // 2
1844114, // 3
15206720, // 4
7674351, // 5
5390669, // 6
5420431, // 7
9028256, // 8
15752490, // 9
13039082 // 10
};
Comparator cmp = new MyComparator();
Arrays.sort(a,cmp);//排序
for(int i=0;i<a.length;i++)
System.out.print(a[i]+" "); //从小到大 依次排序
}
static class MyComparator implements Comparator<Integer> {
@Override
public int compare(Integer o1, Integer o2) {
//如果n1小于n2,我们就返回正值,如果n1大于n2我们就返回负值,
//这样颠倒一下,就可以实现反向排序了
if (o1 < o2) {
return 1;
} else if (o1 > o2) {
return -1;
} else {
return 0;
}
}
}
}
网友评论