首页 > 代码库 > 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 降序
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。