list,ArrayList集合进行排序

首先  pojo里需要 Comparator接口,实现 int compare(Object o1, Object o2)方法


    @Override
    public int compare(Object o1, Object o2)
    {
        ThemePojo themePojo1=  (ThemePojo)o1;
        ThemePojo themePojo2=  (ThemePojo)o1;
        if(themePojo1.getId() > themePojo2.getId())
        {
            return 1;
        }
        return 0;
    }
    

然后在具体需要排序的地方增加如下代码  


 // 对主题内容进行排序
        Comparator<ThemePojo> comparator = new Comparator<ThemePojo>() {
            public int compare(ThemePojo t1, ThemePojo t2) {
                // 先排id
                if (t1.getId() != t2.getId()) {
                    return t1.getId() - t2.getId();
                }else
                {
                    t1.getCreateTime().compareTo(t2.getCreateTime());
                }
                return  t1.getId() - t2.getId(); 
            }
        };
        Collections.sort(listPojo, comparator);
        Timestamp createTime = null;
        for(ThemePojo themePojo:listPojo)
        {
            createTime =  themePojo.getCreateTime();
        }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值