标签:style blog color io os ar div sp cti
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); } }
标签:style blog color io os ar div sp cti
原文地址:http://www.cnblogs.com/muzhongjiang/p/3990539.html