public void testsort4() {
List<Student> studentList = new ArrayList<Student>();
List<String> idList = new ArrayList<String>();
Random random=new Random();
for (int i=0;i<3;i++){
StringBuilder stringBuilder;
do {stringBuilder=new StringBuilder();
Integer num=random.nextInt(1000);
String str=num.toString();
stringBuilder.append(str);
}while (idList.contains(stringBuilder));
idList.add(stringBuilder.toString());
}
studentList.add(new Student(idList.get(0), "Mike"));
studentList.add(new Student(idList.get(1), "Angela"));
studentList.add(new Student(idList.get(2), "Peter"));
System.out.println("------------------排序前-----------------------------");
for (Student a : studentList) {
System.out.println("学生:" + a.id + "," + a.name);
}
System.out.println("---------------按ID排序后-----------------------------");
Collections.sort(studentList);
for (Student a : studentList) {
System.out.println("学生:" + a.id + "," + a.name);
}
System.out.println("---------------按name排序后---------------------------");
Collections.sort(studentList,new StudentComparator());
for (Student a : studentList) {
System.out.println("学生:" + a.id + "," + a.name);
}
}