//先根据学生的成绩降序排序,如果成绩相同再根据年龄升序排序,如果年龄相同再根据名字升序排序
public class ScoreList implements Comparable{
private String name;
private int age;
private int score;
public ScoreList(String name, int age, int score) {
this.name = name;
this.age = age;
this.score = score;
}
@Override
public int compareTo(Object o) {
ScoreList sl = (ScoreList)o;
//return sl.score - this.score; 按分数降序排序
//return this.age - sl.age; 按年龄升序排序
//return this.name.compareTo(sl.name) ; 按名字升序排序
//先按照 分数 再按照 年龄 最后按照 名字 排序
int result = sl.score-this.score;
if(result==0){
result = this.age-sl.age;
}
if (result==0){
result=this.name.compareTo(sl.name);
}
return result;
}
}
标签:Comparable,name,int,age,接口,score,sl,排序 From: https://www.cnblogs.com/Sco-/p/16990064.html