这里有个实例
import java.util.*;
class Student implements Comparable<Object> {
public Student(String name,int age,int score)
{
this.name=name;
this.age=age;
this.score=score;
}
private String name;
public String Getname()
{
return name;
}
public void Setname(String name)
{
this.name=name;
}
private int age;
public int Getage()
{
return age;
}
public void Setage(int age)
{
this.age=age;
}
private int score;
public int getScore()
{
return score;
}
public void SetScore(int score)
{
this.score=score;
}
public int compareTo(Object o)
{
Student s=((Student)o);
return score>s.getScore()? 1:(score==s.getScore()? 0:-1);
}
}
public class Sortbyscore {
/**
* @param argS
*/
public static void main(String[] argS) {
// tODO Auto-generated method Stub
Student[] stu=new Student[]{
new Student("张三",20,90),
new Student("李四",21,95),
new Student("王五",12,80),
new Student("刘备",23,94),
new Student("赵东",34,88)
};
System.out.println("before sorting: ");
for(int i=0;i<stu.length;i++)
{
System.out.println(" "+stu[i].Getname()+" "+stu[i].Getage()+" "+stu[i].getScore());
}
Arrays.sort(stu);
System.out.println("after sorting: ");
for(int i=0;i<stu.length;i++)
{
System.out.println(" "+stu[i].Getname()+" "+stu[i].Getage()+" "+stu[i].getScore());
}
}
}
|