元素降序排列:
public class Test { public static void main(String[] args){ Integer[] b={9, 8, 7, 2, 3, 4, 1, 0, 6, 5}; Comparator cmp=new MyComparator(); Arrays.sort(b, cmp); for(int i=0; i<b.length; i++){ System.out.print(b[i]+ " "); } } }
public class MyComparator implements Comparator<Integer> { @Override public int compare(Integer o1, Integer o2) { if(o1 < o2){ return 1; }else if(o1 >o2){ return -1; }else{ return 0; } } }
运行结果:9 8 7 6 5 4 3 2 1 0