楼主这种比较是实体类自身实现Comparable接口又称内部比较,List是有序的集合,不会调用内部比较进行排序的。要么你不用ArrayList,改用TreeSet你要排序List可以用外部比较器Collections.sort(list,new Comparator<Student>(){ public int compare(Student s1,Student s2){//自己重写和你的Comparable的compareTo方法一样 } public boolean equals(Student s){} })
Collections.sort(list,Comparable)
public int compare(Student s1,Student s2){//自己重写和你的Comparable的compareTo方法一样
}
public boolean equals(Student s){}
})
你这里要排序用:Collections.sort(list)
public static void main(String agrs[]){
List<Student> allList = new ArrayList<Student>();
Collections.addAll(allList,new Student("张三",21,84));
Collections.addAll(allList,new Student("李四",23,80));
Collections.addAll(allList,new Student("王五",22,89));
Collections.addAll(allList,new Student("孙琦",19,100));
Collections.addAll(allList,new Student("赵妞",20,75));
Collections.sort(allList);
Iterator<Student> iter=allList.iterator() ;
while(iter.hasNext()){
System.out.println(iter.next()+",");
}
}
}
这样就对了!!哈哈谢谢