先补充一下c/c++的思维
bool cmp(int a,int b)
{ return a>b; } //大到小排序
第一种:
o1大 减完为正 o1排前面
前 -- 后 = 增序(小-->大)
后 -- 前 = 降序(大-->小)
第二种:
返回值负数 (正常c++的思维 大-->小)
返回值整数 (c++的思维反过来)
package aaa;
import java.util.Arrays;
import java.util.Comparator;
public class test01 {
public static class MyComp1 implements Comparator<Integer>{
public int compare(Integer o1,Integer o2) {
return o1-o2;
}
}
public static class MyComp2 implements Comparator<Integer>{
public int compare(Integer o1,Integer o2) {
//大到小9 6 5 4 1
if(o1>o2)return -1;
else if(o1<o2)return 1;
else return 0;
}
}
public static void main(String[] args) {
Integer[] a = {5,9,1,4,6};
// Arrays.sort(a);
Arrays.sort(a,new MyComp());
for(int i=0;i<a.length;i++)
System.out.print(a[i]+" ");
}
}