import java.util.Arrays;
class ArrayTest1
{
public static void main(String[] args)
{
Student[] ss = new Student[]{new Student("henry",28),new Student("ben",26),new Student("William",38)};
Arrays.sort(ss);
for(int i = 0;i<ss.length;i++)
{
System.out.println(ss[i]);
}
}
}
class Student implements Comparable
{
String name;
int age;
Student(String name,int age)
{
this.name = name;
this.age = age;
}
public String toString()
{
return "name="+this.name+",age="+this.age;
}
public int compareTo(Object o)
{
Student s = (Student)o;
int result;
result = name.compareTo(s.name);
/*int result;
result = age>s.age ? 1 : (age==s.age ? 0 : -1);
if(result==0)
{
result = name.compareTo(s.name);
}*/
return result;
}
}我想让Student数据只根据NAME排序,以上的程序达不到想要的效果。但是根据AGE排序却没问题,不知道为什么