sort(T[] a, Comparator<? super T> c)
用这个函数,自定义comparator函数实现最后一个值排序
import java.util.Arrays;
import java.util.Comparator;
public class TestDengCha {
public static void main(String[] agrs) {
Student[] s = new Student[5];
s[0] = new Student("西西", 59, 78, 80);
s[1] = new Student("小亿", 67, 78, 70);
s[2] = new Student("咚咚", 23, 84, 77);
s[3] = new Student("李济", 69, 78, 73);
s[4] = new Student("冰芳", 76, 74, 71);
Arrays.sort(s, new Comparator<Student>() {
@Override
public int compare(Student s1, Student s2) {
if (s1 != null && s2 != null) {
return s1.getEnglish() > s2.getEnglish() ? 1 : (s1.getEnglish() == s2.getEnglish() ? 0 : -1);
}
return 0;
}
});
System.out.println(Arrays.toString(s));
}
}
class Student {
private String name;
private int chinese;
private int math;
private int english;
Student(String name, int chinese, int math, int english) {
this.name = name;
this.chinese = chinese;
this.english = english;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getChinese() {
return chinese;
}
public void setChinese(int chinese) {
this.chinese = chinese;
}
public int getMath() {
return math;
}
public void setMath(int math) {
this.math = math;
}
public int getEnglish() {
return english;
}
public void setEnglish(int english) {
this.english = english;
}
@Override
public String toString() {
return "姓名:" + name + " 语文" + chinese + " 数学" + math + " 英语" + english;
}
}