1.comparator接口
重写compare函数
@Override
public int compare(Student o1, Student o2) {
if(o1.score>o2.score)
return -1;
else
return 1;
}
应用:
Collections.sort(myArrayList,new Student());
2.comparable接口
重写compareTo方法
@Override
public int compareTo(Student o) {
return this.score-o.score;
}
运用: Collections.sort(myArrayList);