public static void main(String[] args) { Student []array= new Student[]{ new Student(1,"小王","二年级",56,5), new Student(2,"小李","二年级",57,5), new Student(3,"小张","二年级",76,5), new Student(4,"小孙","二年级",47,5) }; Arrays.sort(array); for (int i = 0; i < array.length; i++) { System.out.println(array[i]); } }
}
import java.util.Arrays; public class Student implements Comparable { public int id; public String name; public String grade; public double score; public int age; public Student(int id, String name, String grade, double score, int age) { super(); this.id = id; this.name = name; this.grade = grade; this.score = score; this.age = age; } @Override public int compareTo(Object o) { Student student=(Student)o; if(this.score>student.score){ return 1; } else{ if(this.score return -1; } else if(this.id return 1; } else if(this.id return -1; } return 0; } } public String toString (){ String str=this.id+" "+this.name+" "+this.score+" "+this.grade+" "+this.age; return str; } }