需要用到比较器Comparator
模板:
package test;
import java.util.Arrays;
import java.util.Comparator;
public class Test {
static Comparator<Integer> cmp = new Comparator<Integer>() {
public int compare(Integer a, Integer b) {
return b-a;
}
};
public static void main(String[] args) {
Integer[] a= {3,1,2,4};
Arrays.sort(a,cmp);//不用cmp是从小到大排序
for (int i : a) {
System.out.println(i);
}
}
}
输出4 3 2 1