主题:关于对象数组的排序,请教!
请大虾帮我解释下这个对象数组排序是怎么实现的,我主要看不懂其中红色的方法是如何实现的,不知道那个方法中是哪个对象传给Object o, 问号语句中的s.num和num又分别是什么?
class ArrayTest
{
public static void main(String[] args)
{
Student[] st=new Student[]{new Student("zhagnshan",3),
new Student("lishi",2),
new Student("wangwu",1)};
java.util.Arrays.sort(st);
for(int i=0;i<st.length;i++)
{
System.out.println(st[i]);
}
}
}
class Student implements Comparable
{
int num;
String name;
Student(String name,int num)
{
this.name=name;
this.num=num;
}
public String toString()
{
return "num "+num+" is "+name;
}
[color=FF0000]public int compareTo(Object o)
{
Student s=(Student)o;
return num>s.num?1:(num==s.num?0:-1);
}[/color]}
class ArrayTest
{
public static void main(String[] args)
{
Student[] st=new Student[]{new Student("zhagnshan",3),
new Student("lishi",2),
new Student("wangwu",1)};
java.util.Arrays.sort(st);
for(int i=0;i<st.length;i++)
{
System.out.println(st[i]);
}
}
}
class Student implements Comparable
{
int num;
String name;
Student(String name,int num)
{
this.name=name;
this.num=num;
}
public String toString()
{
return "num "+num+" is "+name;
}
[color=FF0000]public int compareTo(Object o)
{
Student s=(Student)o;
return num>s.num?1:(num==s.num?0:-1);
}[/color]}