如何对时间戳列表进行排序?

编程入门 行业动态 更新时间:2024-10-24 16:31:44
本文介绍了如何对时间戳列表进行排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个timestamp(long)列表,其中我必须根据时间或添加的列表对列表进行排序.

I have a list of timestamp(long) in which I have to sort list base on time or list added.

我已经尝试并搜索了,但是现在可以正常工作了.

I have tried and searched but its now working.

Collections.sort(vehicles, new Comparator<VehicleListModel.Vehicle>() { @Override public int compare(VehicleListModel.Vehicle o1, VehicleListModel.Vehicle o2) { try { DateFormat format = new SimpleDateFormat("MM-dd-yyyy hh:mm:ss"); return format.parse(o1.getCreated())pareTo(format.parse(o2.getCreated())); } catch (Exception e) { e.printStackTrace(); return 0; } } }); customerListAdapter.notifyDataSetChanged();

它不起作用,然后我尝试了 此,但此Date(timeStamp)已弃用

its not working then I tried this but this Date(timeStamp) is deprecated

请帮助

推荐答案

如果getCreated返回Date,则可以使用compareTo比较两个日期,并根据此比较对列表进行排序,例如:

If getCreated returns a Date then you can compare two dates using compareTo and sort the list based on this comparison, e.g.:

List<VehicleListModel.Vehicle> list = new ArrayList<>(); list.sort((e1, e2) -> e1.getCreated()pareTo(e2.getCreated()));

更新

如果getCreated返回long值,则可以将其装箱并使用Long类的compareTo方法,例如:

If getCreated returns long value then you can box it and use compareTo method of Long class, e.g.:

List<ChainCode> list = new ArrayList<ChainCode>(); list.sort((e1, e2) -> new Long(e1.getCreated())pareTo(new Long(e2.getCreated())));

更多推荐

如何对时间戳列表进行排序?

本文发布于:2023-10-24 09:41:32,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1523600.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:时间   列表

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!