日期:2014-05-20 浏览次数:20876 次
import java.util.*; [code]import java.util.*; class StuScoreSort { public static void main(String[] args) { Student stu1 = new Student("成龙",28,100); Student stu2 = new Student("李连杰",32,95); Student stu3 = new Student("洪金宝",35,92); Student stu4 = new Student("杨紫琼",25,88); Student stu5 = new Student("元奎",36,83); ArrayList<Student> list = new ArrayList<Student>(); list.add(stu1); list.add(stu2); list.add(stu3); list.add(stu4); list.add(stu5); Collections.sort(list,new MyComparator()); Iterator<Student> it = list.iterator(); while(it.hasNext()) { Student s = it.next(); System.out.println(s.getName()+","+s.getAge()+","+s.getScores()); } } } class Student { private String name; private int age; private int scores; Student() {} Student(String name,int age,int scores) { this.name = name; this.age = age; this.scores = scores; } public void setName(String name) { this.name = name; } public void setAge(int age) { this.age = age; } public void setScroes(int scores) { this.scores = scores; } public String getName() { return name; } public int getAge() { return age; } public int getScores() { return scores; } } class MyComparator implements Comparator { public int compare(Object o1,Object o2) { Student s1 = (Student)o1; Student s2 = (Student)o2; if(s1.getScores()<s2.getScores()) return -1; else if(s1.getScores()==s2.getScores()) return 0; else return 1; } }
class MyComparator implements Comparator<Student> { @Override public int compare(Strudent s1, Student s2) { // ... } }
------解决方案--------------------
class MyComparator implements Comparator
{
public int compare(Object o1,Object o2)
{
Student s1 = (Student)o1;
Student s2 = (Student)o2;
class MyComparator implements Comparator<Student> {
public int compare(Student o1, Student o2) {
Student s1 = o1;
Student s2 = o2;