📄 studentagesort.java
字号:
/*
* StudentAgeSort
* 2007
* 按学生的年龄排序
*/
public class StudentAgeSort extends GenericSort {
public int compare(Object o1, Object o2) {
int o1Age = ((Student) o1).getAge();
int o2Age = ((Student) o2).getAge();
int result = 0;
if (o1Age > o2Age) {
result = 1;
} else if (o1Age < o2Age) {
result = -1;
}
return result;
}
/**
* @param args
*/
public static void main(String[] args) {
Student stu1 = new Student();
stu1.setName("王二");
stu1.setAge(17);
Student stu2 = new Student();
stu2.setName("张三");
stu2.setAge(15);
Student stu3 = new Student();
stu3.setName("李四");
stu3.setAge(19);
Student stu4 = new Student();
stu4.setName("赵六");
stu4.setAge(18);
Student[] stus = { stu1, stu2, stu3, stu4 };
Sort sort = new StudentAgeSort();
sort.upSort(stus);
for (int i = 0; i < stus.length; i++) {
System.out.println(((Student) stus[i]).getName());
}
sort.downSort(stus);
for (int i = 0; i < stus.length; i++) {
System.out.println(((Student) stus[i]).getName());
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -