Collections.sort(list);
System.out.println("按成绩排序后:" + list);
Collections.reverse(list);
System.out.println("按成绩逆序排列:" + list);
Iterator<Student> it = list.iterator();
while (it.hasNext()) {
Student student = it.next();
System.out.println(student.name + " " + student.score);
}此代码实现不了5位学生的成绩排序,求解.

解决方案 »

  1.   

    Student不是一个可推测排序关系的类,你确定你的 Student 实现了自己的 Comparable 接口么?可是试试这个:
    Collections.sort(list, new Comparator() {

    public int compare(Object o1, Object o2) {
    Student s1 = (Student)o1;
    Student s2 = (Student)o2;
    // 我也不晓得你要咋比较,自己写吧
    return 0;
    }
    })
      

  2.   


            List list = new ArrayList();
            Collections.sort(list, new Comparator() {            public int compare(Object o1, Object o2) {
                    Student s1 = (Student) o1;
                    Student s2 = (Student) o2;
                    if (s1.score > s2.score)
                        return 1;
                    else if (s1.score > s2.score)
                        return -1;
                    return 0;
                }        });
            System.out.println("按成绩排序后:" + list);
            Collections.reverse(list);
            System.out.println("按成绩逆序排列:" + list);
            Iterator<Student> it = list.iterator();
            while (it.hasNext()) {
                Student student = it.next();
                System.out.println(student.name + " " + student.score);
            }