方法一:实现Comparator接口,并重写compare方法
- 实体类代码:
import java.util.Comparator;
/**
* 学生类 方法一
* 实现Comparator接口
* 并重写compare方法
* @author liaot
*
*/
public class Student implements Comparator<Student>{
private String name; //姓名
private int age; //年龄
//重写 比较方法 本次例子定义为按年龄比较
@Override
public int compare(Student o1, Student o2) {
if(o1.getAge() > o2.getAge()){
return 1;
}else{
return -1;
}
}
public Student(String name, int age) {
super();
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 测试类:
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class Main {
public static void main(String[] args) {
//初始化四个不同的学生
Student stu1 = new Student("路人甲", 20);
Student stu2 = new Student("路人已", 18);
Student stu3 = new Student("路人丙", 16);
Student stu4 = new Student("路人丁", 19);
//新建List把学生加进List
List<Student> stuList = new ArrayList<>();
stuList.add(stu1);
stuList.add(stu2);
stuList.add(stu3);
stuList.add(stu4);
System.out.println("排序前:=====");
for(Student stu :stuList){
System.out.println("姓名:"+stu.getName() +" 年龄"+stu.getAge());
}
//排序
Collections.sort(stuList, stu1); //第一个参数为List 第二个参数为对象的一个实例
System.out.println("排序后:=====");
for(Student stu :stuList){
System.out.println("姓名:"+stu.getName() +" 年龄"+stu.getAge());
}
}
}