import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
public class students_test {
public static void main(String[] args) {
// TODO Auto-generated method stub
List<Student> l = new ArrayList<Student>();
l.add(new Student(18,"Lihua", "12545"));
l.add(new Student(20, "xiaohong", "154785"));
l.add(new Student(65, "xiaoMing", "54785"));
Collections.sort(l, new Comparator<Student>() {
@Override
public int compare(Student o1, Student o2) {
// TODO Auto-generated method stub
if(o1.id.compareTo(o2.id) == 0)
{
return o1.age-o2.age;//1在前从小到大,2在前从大到小
}
return o1.id.compareTo(o1.id);
}
});
Iterator<Student> it = l.iterator();
while(it.hasNext())
{
System.out.println(it.next());
}
}
}
java List 排序
最新推荐文章于 2023-04-10 17:01:09 发布