首页 > 代码库 > Collections sort 降序

Collections sort 降序

 

public class User implements Comparable<User> {    private long id;    private int age;    public User(long id, int age) {        this.id = id;        this.age = age;    }    public long getId() {        return id;    }    public void setId(long id) {        this.id = id;    }    public int getAge() {        return age;    }    public void setAge(int age) {        this.age = age;    }    @Override    public int compareTo(User obj) {        if (this.age > obj.age) {            return 1;        } else if (this.age < obj.age) {            return -1;        }        return 0;    }    @Override    public String toString() {        return "User [id=" + id + ", age=" + age + "]";    }}

=====================================================================================================

public class Test {    public static void main(String[] args) {        List<User> list = new ArrayList<User>();        list.add(new User(1, 3));        list.add(new User(1, 2));        list.add(new User(1, 4));        Collections.sort(list);        Collections.reverse(list);        System.err.println(list);    }}

 

Collections sort 降序