课上补做-2
要求:针对下面的Student类,使用Comparator编程完成以下功能:
- 在测试类StudentTest中新建学生列表,包括自己和学号前后各两名学生,共5名学生,给出运行结果(排序前,排序后)
- 对这5名同学分别用学号和总成绩进行增序排序,提交两个Comparator的代码
- 课下提交代码到码云
import java.util.Comparator;
public class IDComparator implements Comparator {
@Override
public int compare(Object o1, Object o2) {
Student st1 = (Student)o1;
Student st2 = (Student)o2;
return (Integer.parseInt(st1.getId())-Integer.parseInt(st2.getId()));
}
}
import java.util.Comparator;
public class scoreComparator implements Comparator {
@Override
public int compare(Object o1, Object o2) {
Student st1 = (Student)o1;
Student st2 = (Student)o2;
return (int) (st1.getTotalScore()-st2.getTotalScore());
}
}
课后习题
代码上传
https://gitee.com/BESTI-IS-JAVA-2018/20165337/tree/master/week10buzuo