2021-05-10

Demo

/**
* @qvthor liuwenzheng
* @date 2021/5/10 10:25
*/
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",70);
       User wangwu = new User(17,"wangwu",88);
       list.add(zhangsan);
       list.add(lisi);
       list.add(wangwu);
       System.out.println(list);
       System.out.println(list);

       Collections.sort(list, new Comparator() {
           @Override
           public int compare(User o1, User o2) {
               return o1.getUid()-o2.getUid();
           }
       });
   }

}

User

package sort;

/**
 * @qvthor liuwenzheng
 * @date 2021/5/10 10:19
 */
// 排序
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.grade = grade ;
        this.name = name ;
    }

    public Integer getUid() {
        return uid;
    }

    public String getName() {
        return name;
    }

    public Integer getGrade() {
        return grade;
    }

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

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

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

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

//    @Override
//    public int compareTo(Object o) {
//        return 0;
//    }
}

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