求对以下list数据进行按时间倒序排列

程序猿的那点事 2016-12-22 04:26:09
Item item = new Item();
item.name = "jack";
item.date= "2016-11-30 21:20:12";

Item item = new Item();
item.name = "tom";
item.date= "2016-12-30 21:20:12";

Item item = new Item();
item.name = "jack";
item.date= "2016-10-30 21:20:12";

Item item = new Item();
item.name = "jack";
item.date= "2016-12-31 21:20:12";

List<Item> item = new ArrayList<>();
item.add(item);
...全文
1008 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
寒冰大神 2016-12-23
  • 打赏
  • 举报
回复
Collections.sort(itemlist, new Comparator<Item>() { @Override public int compare(Item item, Item t1) { SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date date1 = null; Date date2 = null; try { date1 = new Date(format.parse(item.date).getTime()); date2 = new Date(format.parse(t1.date).getTime()); }catch (ParseException e){ } // 对日期字段进行升序,如果欲降序可采用after方法 if (date1.before(date2)) { return 1; } return -1; } });
寒冰大神 2016-12-23
  • 打赏
  • 举报
回复
Item item1 = new Item(); item1.name = "jack"; item1.date = "2016-11-30 21:20:12"; Item item2 = new Item(); item2.name = "tom"; item2.date = "2016-12-30 21:20:12"; Item item3 = new Item(); item3.name = "jack"; item3.date = "2016-10-30 21:20:12"; Item item4 = new Item(); item4.name = "jack"; item4.date = "2016-12-31 21:20:12"; List<Item> itemlist = new ArrayList<>(); itemlist.add(item1); itemlist.add(item2); itemlist.add(item3); itemlist.add(item4); Collections.sort(itemlist, new Comparator<Item>() { @Override public int compare(Item item, Item t1) { Date date1 = TimeUtils.string2Date(item.date); Date date2 = TimeUtils.string2Date(t1.date); // 对日期字段进行升序,如果欲降序可采用after方法 if (date1.before(date2)) { return 1; } return -1; } }); for (Item s : itemlist) { Log.e("date", s.date); }
  • 打赏
  • 举报
回复
引用 1 楼 qq_34364155 的回复:
实现Comparator接口 按时间 进行排序 Collections.sort(item, new Comparator<Item >(){ public int compare(Item arg0, Item arg1) { } }
public int compare(Item arg0, Item arg1) { }这里面应该怎么写呢
寒冰大神 2016-12-22
  • 打赏
  • 举报
回复
实现Comparator接口 按时间 进行排序 Collections.sort(item, new Comparator<Item >(){ public int compare(Item arg0, Item arg1) { } }

80,349

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧