如何对时间戳列表进行排序? [英] How to sort timestamp list?

查看:1915
本文介绍了如何对时间戳列表进行排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个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()).compareTo(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().compareTo(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()).compareTo(new Long(e2.getCreated())));

这篇关于如何对时间戳列表进行排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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