您的位置:首页 > 其它

Comparator进行排序

2014-01-24 13:40 211 查看
基础学习: http://www.360doc.com/content/08/0928/10/16915_1686535.shtml


比如我们有一个paymentVO 里面有好多属性 比如我们的需求就是根据里面的paymentSequenceNo进行升序、降序排列展示出来。

@SuppressWarnings({ "unchecked", "rawtypes" })
public static void ascendingSort(List<PaymentVO> list) {
if (!list.isEmpty()) {
Collections.sort(list,new Comparator() {
@Override
public int compare(Object o1, Object o2) {
PaymentVO nVo1 = (PaymentVO) o1;
PaymentVO nVo2 = (PaymentVO) o2;
return nVo1.getBankTradePaymentSeqNo().compareTo(nVo2.getBankTradePaymentSeqNo());
}});
}
}


同理降序排列只要把上面的Collections.sort(..)改成Collections.reverse(...)即可.

@SuppressWarnings({ "unchecked", "rawtypes" })
public static void desscendingSort(List<PaymentVO> list) {
if (!list.isEmpty()) {
Collections.reverse(list,new Comparator() {
@Override
public int compare(Object o1, Object o2) {
PaymentVO nVo1 = (PaymentVO) o1;
PaymentVO nVo2 = (PaymentVO) o2;
return nVo1.getBankTradePaymentSeqNo().compareTo(nVo2.getBankTradePaymentSeqNo());
}});
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: