Java ArrayList custom sorting

ArrayList implements custom sorting mainly through two methods

  1. The object class that needs to be sorted implements the Comparable interface, rewrites the compareTo(Object o) method, and defines the sorting rules in it, then you can directly call Collections.sort() to sort the object array.
  2. Implement the comparator interface Comparator, rewrite the compare method, and pass it directly into sort as a parameter

The first method is implemented.
First define an object class and implement the Comparable interface, and then override the compareTo(Object o) method.

Student class

public class Student implements Comparable{

    private String name;
    private int age;
    private String sex;

    public Student(){}

    /*
     * 有参构造方法
     */
    public Student(String name, int age, String sex) {
        super();
        this.name = name;
        this.age = age;
        this.sex = sex;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    /*
     * (non-Javadoc)
     * @see java.lang.Comparable#compareTo(java.lang.Object)
     * 比较年龄大小
     */
    @Override
    public int compareTo(Object o) {
        // TODO Auto-generated method stub
        Student s = (Student)o;
        if(this.age>s.age){
            return 1;
        }else if(this.age<s.age){
            return -1;
        }
        return 0;
    }
}

testing method

public class Sort {

    public static void main(String[] args){

        List<Student> list = new ArrayList<>();
        list.add(new Student("bobby",24,"男"));
        list.add(new Student("test",22,"男"));
        list.add(new Student("tom",12,"男"));
        list.add(new Student("bill",34,"男"));
        list.add(new Student("lucy",54,"女"));
        list.add(new Student("bear",29,"女"));
        System.out.println("-----排序之前-----");
        for(Student s:list){
            System.out.println("姓名:"+s.getName()+"年龄:"+s.getAge()+"性别:"+s.getSex());
        }

        System.out.println("--------------");

        Collections.sort(list);
        System.out.println("-----排序之后-----");
        for(Student s:list){
            System.out.println("姓名:"+s.getName()+"年龄:"+s.getAge()+"性别:"+s.getSex());
        }
    }

}

output result

-----排序之前-----
姓名:bobby年龄:24性别:男
姓名:test年龄:22性别:男
姓名:tom年龄:12性别:男
姓名:bill年龄:34性别:男
姓名:lucy年龄:54性别:女
姓名:bear年龄:29性别:女
-------------------
-----排序之后-----
姓名:tom年龄:12性别:男
姓名:test年龄:22性别:男
姓名:bobby年龄:24性别:男
姓名:bear年龄:29性别:女
姓名:bill年龄:34性别:男
姓名:lucy年龄:54性别:女

The second method
implements the comparator interface Comparator, rewrites the compare method, and directly passes it into sort as a parameter. Use methods of inner classes.

public static void main(String[] args){

        List<Student> list = new ArrayList<>();
        list.add(new Student("bobby",24,"男"));
        list.add(new Student("test",22,"男"));
        list.add(new Student("tom",12,"男"));
        list.add(new Student("bill",34,"男"));
        list.add(new Student("lucy",54,"女"));
        list.add(new Student("bear",29,"女"));

        System.out.println("-----未排序之前-----");
        for(Student s:list){
            System.out.println("姓名:"+s.getName()+"年龄:"+s.getAge()+"性别:"+s.getSex());
        }

        System.out.println("-------------------");

        Collections.sort(list,new Comparator<Student>(){

            @Override
            public int compare(Student o1, Student o2) {
                // TODO Auto-generated method stub
                if(o1.getAge()>=o2.getAge()){
                    return 1;
                }else{
                    return -1;
                }
            }
        });

        System.out.println("-----排序之后-----");
        for(Student s:list){
            System.out.println("姓名:"+s.getName()+"年龄:"+s.getAge()+"性别:"+s.getSex());
        }

    }

output result

-----未排序之前-----
姓名:bobby年龄:24性别:男
姓名:test年龄:22性别:男
姓名:tom年龄:12性别:男
姓名:bill年龄:34性别:男
姓名:lucy年龄:54性别:女
姓名:bear年龄:29性别:女
-------------------
-----排序之后-----
姓名:tom年龄:12性别:男
姓名:test年龄:22性别:男
姓名:bobby年龄:24性别:男
姓名:bear年龄:29性别:女
姓名:bill年龄:34性别:男
姓名:lucy年龄:54性别:女

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325385802&siteId=291194637