import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
public class test {
public static void main(String[] args) {
Student s1 = new Student("张三",3600,22);
Student s2 = new Student("李四",7000,18);
Student s3 = new Student("王五",3600,18);
Student s4 = new Student("赵六",7000,26);
Student s5 = new Student("胜7",9000,53);
Solution solution = new Solution();
Student[] arr1 = {s1, s2,s3,s4,s5};
Student[] sort1 = solution.sort1(arr1);
for(Student s: sort1){
System.out.println(s);
}
}
public static class Solution {
public Student[] sort1(Student[] nums) {
Arrays.sort(nums, new Comparator<Student>() {
@Override
public int compare(Student o1, Student o2) {
if(o1.salary > o2.salary){
return 1;//根据薪水升序排列
}else if(o1.salary < o2.salary){
return -1;//根据薪水升序排列
}else{//如果薪水相同则根据年龄升序排列
if(o1.age >= o2.age){
return 1;
}else{
return -1;
}
}
}
});
return nums;
}
}
}
只需要调用Arrays.sort方法重新实现compare方法,在里面自定义排序规则。