按日期排序有豆日期成员的数组 [英] sorting by date an array of bean having date member

查看:148
本文介绍了按日期排序有豆日期成员的数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个bean类像

public class ItemBean{
    String item_id, item_title, image_url, link_url, description, publish_date,
        in_app_date, sub_section_id, type, duration, orig_url, sync;
    public ItemBean() {  ....  }
    ....
    ....
}

现在在活动中,我得到itemBeans的价值观,使清单;
在比较中使用。你可以看到我的code;

now in activity, i am getting values of itemBeans and making list; to use in comparator. You can see my code;

public void sortDate() {
    List<ItemBean> listIB = new ArrayList<ItemBean>();
    for(int i=0; i<DispLibActivity.itemListVect.size(); i++)
    {
        listIB.add(DispLibActivity.itemListVect.get(i));
    }
    //      Sorting  Call
    Collections.sort(listIB, ItemSimpleListActivity.compareDate);
    itemBeans = new ItemBean[listIB.size()];
    for (int i=0; i<listIB.size(); i++)
    {
        itemBeans[i] = listIB.get(i);
        System.out.println("Date: "+itemBeans[i].publish_date);
    }
}

我比较

public static Comparator<ItemBean> compareDate = new Comparator<ItemBean>() {
    public int compare(ItemBean one, ItemBean other) {
        return one.publish_date.compareTo(other.publish_date);
    }
}

不过,我得到分类收集如下: -
日期:2009年1月1日08:00:00 CST
日期:2011年10月1日08:00:00 CST
日期:2011年10月1日08:00:00 CST
日期:2010年12月2日08:00:00 CST
日期:2009年4月1日08:00:00 CST
日期:2011年4月18日08:00:00 CST
日期:2010年4月6日08:00:00 CST
日期:2010年4月6日08:00:00 CST
日期:2011年9月20日08:00:00 CST
日期:2011年9月20日08:00:00 CST
日期:2011年9月20日08:00:00 CST

But, i am getting sorted collection as following :- Date: 1-1-2009 08:00:00 CST Date: 10-1-2011 08:00:00 CST Date: 10-1-2011 08:00:00 CST Date: 12-2-2010 08:00:00 CST Date: 4-1-2009 08:00:00 CST Date: 4-18-2011 08:00:00 CST Date: 4-6-2010 08:00:00 CST Date: 4-6-2010 08:00:00 CST Date: 9-20-2011 08:00:00 CST Date: 9-20-2011 08:00:00 CST Date: 9-20-2011 08:00:00 CST

这是不妥当的排序顺序。

Which is not proper sorting order.

谁能告诉我的替代方式,或这有什么code中的错误吗?

Can anyone tell me the alternate ways or what is the bug with this code?

推荐答案

您要比较的日期,不是字符串(出版日期)。因此,在比较的方法()您解析字符串日起Date对象(例如,通过的SimpleDateFormat),并通过Date.compareTo这两个日期比较

You have to compare dates and not strings (publish_date). So in method compare() you parse the String date into a Date object (e.g. via SimpleDateFormat) and compare those two dates via Date.compareTo

这篇关于按日期排序有豆日期成员的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆