2021-05-15

1.建文件夹sort
建Java User

public class User /*implements Comparable*/ {
    private Integer uid;
    private String name;
    private Integer grade;

    public User() {
    }

    public User(Integer uid, String name, Integer grade) {
        this.uid = uid;
        this.name = name;
        this.grade = grade;
    }

    public Integer getUid() {
        return uid;
    }

    public void setUid(Integer uid) {
        this.uid = uid;
    }

    public String getName() {
        return name;
    }

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

    public Integer getGrade() {
        return grade;
    }

    public void setGrade(Integer grade) {
        this.grade = grade;
    }

    @Override
    public String toString() {
        return "User{" +
                "uid=" + uid +
                ", name='" + name + '\'' +
                ", grade=" + grade +
                '}';
    }

   @Override
    public int compareTo(User o) {
        return this.getGrade() - o.getGrade() ;
    }
}

2.建Java Demo

public class Demo {
    public static void main(String[] args) {
        ArrayList list = new ArrayList<>();
        User zhangsan = new User(15, "zhangsan", 98);
        User lisi = new User(13, "lisi", 88);
        User wangwu = new User(16, "wangwu", 68);
        list.add(zhangsan);
        list.add(lisi);
        list.add(wangwu);
        System.out.println(list);
        Collections.sort(list, new Comparator() {
            @Override
            public int compare(User o1, User o2) {
                return o1.getUid() - o2.getUid();
            }
        });
        System.out.println(list);


    }
}

你可能感兴趣的:(2021-05-15)