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);
}
});
本文介绍如何利用Collections.sort方法和自定义的Comparator接口,实现根据Value对象中orderId属性对List进行降序排序的操作。
1万+

被折叠的 条评论
为什么被折叠?



