Java Comparator
1. Javadoc解释
int compare(T o1, T o2)
Compares its two arguments for order. Returns a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second.
Arrays.sort(a[], new Comparator<Object>() {
@Override
public int compare(Object o1, Object o2) {
//如果o1<o2,o1=o2,o1>o2分别返回-1,0,1;
//返回-1表示o1小于o2,第一个的参数小于第二个的参数,说明使升序;
//返回1表示o1大于o2,第一个的参数大于第二个的参数,说明使降序;
return o1 - o2; //默认o1-o2是升序,o2-o1是降序;
}
});
2. 举例1:
Dog[] dogs = {
new Dog("W001", 18,100);
new Dog("W002", 15,200);
new Dog("W003", 10,260);
}
Comparator<Dog> dogComparator = new Comparator<Dog>() {
@Override
public int