提示:总成绩的排序
文章目录
成绩排序功能
public void sort(ArrayList<Students>array){
TreeSet<Students>ts=new TreeSet<Students>(new Comparator<Students>() {
@Override//比较器
public int compare(Students o1, Students o2) {
//int num= (o2.getMath()+o2.getChinese())-(o1.getMath()+o1.getChinese());
int num=o2.getsum()-o1.getsum();//看总分,升序
int num1=num==0?o2.getChinese()-o1.getChinese():num;//总分一样看语文
int num2=num1==0?o1.getName().compareTo(o2.getName()):num1;//语文总分一样看名字
return num2;
}
});
ts.addAll(array);
int i=0;
for(Students s:ts){
i++;
System.out.println("第"+i+"名"+"\t"+s.getId()+"\t"+s.getName()+"\t"+s.getAge()+"岁"+"\t"+s.getSex()+"\t"+s.getMath()+"\t"+s.getChinese()+"\t"+s.getsum());
}
}
其中getsum()在Students类里面
public int getsum(){
return this.getChinese()+this.getMath();
}
界面展示
之后,我会做个前端的页面来实现它