ITeye博客

Collections.sort list排序

xinlanzero · 2011-07-23 13:40 · J2EE · 编程语言 · Markdown

newList是个Value对象的List,orderId是Value对象的一个属性,现在要求根据orderId把List里的记录降序排序。使用 Collections.sort方法可实现此要求,需要自己写类实现Comparator的 compare接口,这里采用的内部类的方式,当然也可使用其他方式。

Collections.sort(newList, new Comparator<PropertyValue>(){
 public int compare(Value p1, Value p2) {
     final int count1 = p1.getOrderId();
     final int count2 = p2.getOrderId();
     return (count1 < count2 ? 1 : -1);
    }
});

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。