package com.bjpowernode.t03sort;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
/*
* 自定义比较器的用法
*/
public class TestSort03 {
public static void main(String[] args) {
ArrayList<Student> list = new ArrayList<>();
list.add(new Student(300, "jack"));
list.add(new Student(500, "david"));
list.add(new Student(100, "paul"));
list.add(new Student(600, "wade"));
// 参数一:待排序的list
// 参数二:自定义的比较器
// 该比较器是根据id进行排序的
// Collections.sort(list, new MySort1());
// 根据姓名进行排序
// Collections.sort(list, new MySort2());
// 匿名内部类
// 根据id排序
Collections.sort(list, new Comparator<Student>() {
@Override
public int compare(Student s1, Student s2) {
return s1.getId() - s2.getId();
}
});
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i));
}
}
}