java中实现Comparable接口实现自定义排序

 class Student implements Comparable{
String name;
int gpa;
@Override
public int compareTo(Object arg0) {
// TODO Auto-generated method stub
Student s = (Student)arg0; if(gpa == s.gpa) return name.compareTo(s.name);
else if(gpa < s.gpa) return -1;
else if(gpa > s.gpa) return 1;
else return 0; } } class Test {
public static void main(String[] args){
Scanner in = new Scanner(System.in); int n = in.nextInt(); Student[] s = new Student[n]; for(int i = 0;i < n;i ++){
s[i] = new Student();
s[i].name = in.next();
s[i].gpa = in.nextInt();
} Arrays.sort(s); for (Student ss : s)
{
System.out.println(ss.name);
System.out.println(ss.gpa);
}
}
}
上一篇:九度 1557:和谐答案 (LIS 变形)


下一篇:debian7 请把标有“Debian GNU/Linux 7.1.0 _Wheezy_ - Official amd64 DVD Binary-1 20130615-23:06”的盘片插入驱动器“/media/cdrom/”再按回车键