import java.util.ArrayList;
import java.util.Collections;public class StudentRanging {
public static void main(String[] args) {
ArrayList<Student> list = new ArrayList<Student>();
Student a = new Student("胖胖", 30, 90.5);
Student b = new Student("刘静", 25, 92.5);
Student c = new Student("苏凯多多", 21, 80.5);
Student f = new Student("阿成才", 24, 80.5);
Student d = new Student("许三多", 28, 90.5);
Student e = new Student("成才", 20, 90.5); list.add(a);
list.add(b);
list.add(c);
list.add(d);
list.add(e);
list.add(f);
Collections.sort(list);
for (Student s : list) {
System.out.println("info:" + s);
}
} public static class Student implements Comparable<Student> {
String name;
int age;
double score; public Student(String name, int age, double score) {
this.name = name;
this.age = age;
this.score = score;
} public String getName() {
return name;
} public int getAge() {
return age;
} public double getScore() {
return score;
} public void setName(String name) {
this.name = name;
} public void setAge(int age) {
this.age = age;
} public void setScore(double score) {
this.score = score;
} @Override
public String toString() {
return name + " " + age + " " + score;
} @Override
public int compareTo(Student o) {
int result = 0; return this.getName().compareTo(o.getName());
} }}

解决方案 »

  1.   

    它的输出为:
    info:刘静 25 92.5
    info:成才 20 90.5
    info:胖胖 30 90.5
    info:苏凯多多 21 80.5
    info:许三多 28 90.5
    info:阿成才 24 80.5
    不是根据姓名的姓来排序的
      

  2.   

    要根据姓名的姓排序comparable怎么写啊
      

  3.   

    这是根据汉字的unicode值来排序的。你要按照汉字拼音的首字母来排序:
    比如:阿成才,成才,刘静,胖胖,苏凯多多,许三多
    那复杂些看这篇博客