码迷,mamicode.com
首页 > 其他好文 > 详细

Collections sort 降序

时间:2014-09-24 17:02:57      阅读:215      评论:0      收藏:0      [点我收藏+]

标签: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);
    }
}

 

Collections sort 降序

标签:style   blog   color   io   os   ar   div   sp   cti   

原文地址:http://www.cnblogs.com/muzhongjiang/p/3990539.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!