美好的一天!
我有一个具有以下属性的对象学生:
class Student
String name
Date birthday
我使用arrayList来存储Student Objects我的问题是,如何使用collecitons排序按生日对StudentList进行排序?
List <Student> studentList = new ArrayList<Student>();
我该如何编码?
Collections.sort(????);
谢谢
您可以传递Comparator
到Collections.sort()
以按生日处理排序:
Collections.sort(studentList, new Comparator<Student>() {
public int compare(Student s1, Student s2) {
return s1.getBirthday().compareTo(s2.getBirthday());
}
});
如果您还没有班级,则需要将其添加getBirthday()
到Student
班级中。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句