/*
* 对其他类型泛型的List进行排序,例如:Student
*/
public void testsort2() {
List<Student> studentList = new ArrayList<Student>();
List<Integer> IntegerList = new ArrayList<Integer>();
Random random = new Random();
Integer k;
for (int i = 0; i < 3; i++) {
do {
k = random.nextInt(10);
} while (IntegerList.contains(k));
IntegerList.add(k);
}
studentList.add(new Student(IntegerList.get(0) + "", "yyz"));
studentList.add(new Student(IntegerList.get(1) + "", "cl"));
studentList.add(new Student(IntegerList.get(2) + "", "zyq"));
System.out.println("--------------排序前--------------");
for (Student student : studentList) {
System.out.println("学生:" + student.id + "," + student.name);
}
Collections.sort(studentList);
System.out.println("--------------排序后--------------");
for (Student student : studentList) {
System.out.println("学生:" + student.id + "," + student.name);
}
}