码迷,mamicode.com
首页 > 编程语言 > 详细

Java中的集合排序

时间:2015-07-09 14:26:05      阅读:121      评论:0      收藏:0      [点我收藏+]

标签:

1. 定义排序

class ComparatorDefault implements Comparator {
        public int compare(Object arg0, Object arg1) {
            OrderDetailReturn.BidsEntity bid0 = (OrderDetailReturn.BidsEntity) arg0;
            OrderDetailReturn.BidsEntity bid1 = (OrderDetailReturn.BidsEntity) arg1;
            //按ID排序
            return bid0.getId() - bid1.getId();
        }
    }

    class ComparatorCredit implements Comparator {
        public int compare(Object arg0, Object arg1) {
            OrderDetailReturn.BidsEntity bid0 = (OrderDetailReturn.BidsEntity) arg0;
            OrderDetailReturn.BidsEntity bid1 = (OrderDetailReturn.BidsEntity) arg1;
            //按ID排序
            if (bid0.getBearer() == null || bid1.getBearer() == null) return 0;
            return bid0.getBearer().getScore() - bid1.getBearer().getScore();
        }
    }

    class ComparatorPrice implements Comparator {
        public int compare(Object arg0, Object arg1) {
            OrderDetailReturn.BidsEntity bid0 = (OrderDetailReturn.BidsEntity) arg0;
            OrderDetailReturn.BidsEntity bid1 = (OrderDetailReturn.BidsEntity) arg1;
            //按ID排序
            return bid0.getPrice() - bid1.getPrice();
        }
    }

2. 调用排序

if(bidList == null) return;
        Comparator comparator;
        if (style == 1) {
            comparator = new ComparatorCredit();
        } else if (style == 2) {
            comparator = new ComparatorPrice();
        } else {
            comparator = new ComparatorDefault();
        }
        Collections.sort(bidList, comparator);

 

Java中的集合排序

标签:

原文地址:http://www.cnblogs.com/atwind/p/4633040.html

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