废话不多说直接上代码:

1.在对象中增加实现排序规则方法

1.对象 实现Comparable 实现compareTo方法

package com.study;  
  
public class Student implements Comparable<Student>{  
      
    private int studentId;  
    private String studentName;  
    private int age;  
  
    public Student(int studentId, String studentName, int age) {  
        this.studentId = studentId;  
        this.studentName = studentName;  
        this.age = age;  
    }  
  
    public int getStudentId() {  
        return studentId;  
    }  
  
    public void setStudentId(int studentId) {  
        this.studentId = studentId;  
    }  
  
    public String getStudentName() {  
        return studentName;  
    }  
  
    public void setStudentName(String studentName) {  
        this.studentName = studentName;  
    }  
  
    public int getAge() {  
        return age;  
    }  
  
    public void setAge(int age) {  
        this.age = age;  
    }  
//实现compareTo方法
    public int compareTo(Student o) {  
        if (age != o.getAge()) {  
            return age - o.getAge();  
        } else if (!studentName.equals(o.getStudentName())) {  
            return studentName.compareTo(o.getStudentName());  
        } else {  
            return studentId - o.getStudentId();  
        }  
    }  
}  

2.排序测试

public class Main {  
  
    public static void main(String[] args) {  
          
        Student stu1 = new Student(1, "zhangsan", 28);  
        Student stu2 = new Student(2, "zhagnsan", 19);  
        Student stu3 = new Student(3, "wangwu", 19);  
        Student stu4 = new Student(4, "wangwu", 19);  
        Student stu5 = new Student(5, "zhaoliu", 18);  
  
        ArrayList<Student> list = new ArrayList<Student>();  
        list.add(stu1);  
        list.add(stu2);  
        list.add(stu3);  
        list.add(stu4);  
        list.add(stu5);  
          
        //这里就会自动根据规则进行排序  
        Collections.sort(list);  
          
        //输出查看结果  
        for (int i = 0; i < list.size(); i++) {  
            Student stu = list.get(i);  
            System.out.println("年龄:" + stu.getAge() + "   姓名:" + stu.getStudentName() + "   学号:" + stu.getStudentId());  
        }  
    }  

2.直接内部类进行排序规则编写,Student没有实现compareTo方法的情况

/** 
 * 实例化一个比较器 
 */  
public class Main {  
  
    public static void main(String[] args) {  
          
        Student stu1 = new Student(1, "zhangsan", 28);  
        Student stu2 = new Student(2, "zhagnsan", 19);  
        Student stu3 = new Student(3, "wangwu", 19);  
        Student stu4 = new Student(4, "wangwu", 19);  
        Student stu5 = new Student(5, "zhaoliu", 18);  
  
        ArrayList<Student> list = new ArrayList<Student>();  
        list.add(stu1);  
        list.add(stu2);  
        list.add(stu3);  
        list.add(stu4);  
        list.add(stu5);  
          
        Comparator<Student> comparator = new Comparator<Student>() {  
            public int compare(Student s1, Student s2) {  
                // 先排年龄  
                if (s1.getAge() != s2.getAge()) {  
                    return s1.getAge() - s2.getAge();  
                } else if (!s1.getStudentName().equals(s2.getStudentName())) {  
                    // 年龄相同则按姓名排序  
                    return s1.getStudentName().compareTo(s2.getStudentName());  
                } else {  
                    // 姓名也相同则按学号排序  
                    return s1.getStudentId() - s2.getStudentId();  
                }  
            }  
        };  
          
        //这里就会自动根据规则进行排序 ,list一定要是arraylist
	//  list = new ArrayList(coll); coll是Collection类型可以这样转换
        Collections.sort(list, comparator);  
        for (int i = 0; i < list.size(); i++) {  
            Student stu = list.get(i);  
            System.out.println("年龄:" + stu.getAge() + "   姓名:" + stu.getStudentName() + "   学号:" + stu.getStudentId());  
        }  
    }  
}  

致谢

参考来源