本文介绍了如何对时间戳列表进行排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个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())));更多推荐
如何对时间戳列表进行排序?
发布评论