普通对象 package ZHANG.API; public class Student{ private String name; private int age; private float score; public Student(String name, int age, float score) { this.name = name; this.age = age; this.score = score; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public float getScore() { return score; } public void setScore(float score) { this.score = score; } public String toString() { return "学生姓名:"+this.name+",年龄:"+this.age+",成绩:"+this.score; } } 比较器 package ZHANG.API; import java.util.Arrays; import java.util.Comparator; public class StudentComparator implements Comparator<Student> { public int compare(Student s1, Student s2) { if(s1.getScore()>s2.getScore()){ return -1; }else if(s1.getScore()<s2.getScore()){ return 1; }else{ return 0; } } public static void main(String[] args) { Student stu[] = {new Student("zhangsan",22,88f), new Student("lisi",25,78f),new Student("wangwu",21,92f), new Student("zhouliu",25,68f),new Student("laoda",26,94f) }; Arrays.sort(stu,new StudentComparator()); for(int i=0;i<stu.length;i++){ System.out.println(stu[i]); } } }