key params span list 什么 one 相同 string
- publicclassStudentimplementsComparable {
- String name;int agepublicintcompareTo(Student another) {int i =0;
- i = name.compareTo(another.name);if(i ==0) {return age - another.age;
- }else {return i;
- }
- }
- }
- 这时我们可以直接用 Collections.sort( StudentList ) 对其排序了.(
- **只需传入要排序的列表**)
- publicclassStudent{
- String name;int age
- }classStudentComparatorimplementsComparator {publicintcompare(Student one, Student another) {int i =0;
- i = one.name.compareTo(another.name);if(i ==0) {return one.age - another.age;
- }else {return i; }
- }
- }
- Collections.sort( StudentList ,new StudentComparator()) 可以对其排序(
- **不仅要传入待排序的列表,还要传入实现了Comparator的类的对象**)
Java 中 Comparable 与 Comparator 的区别
来源: http://www.bubuko.com/infodetail-2015358.html