Java 如何对时间戳列表排序?

Java 如何对时间戳列表排序?,java,android,sorting,date,timestamp,Java,Android,Sorting,Date,Timestamp,我有一个时间戳(long)列表,其中我必须根据时间或添加的列表对列表进行排序 我试过了,也找过了,但它现在起作用了 Collections.sort(vehicles, new Comparator<VehicleListModel.Vehicle>() { @Override public int compare(VehicleListModel.Vehicle o1, VehicleListModel.Vehicl

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

我试过了,也找过了,但它现在起作用了

 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();
Collections.sort(车辆、新比较器(){
@凌驾
公共整数比较(VehicleListModel.VehicleO1,VehicleListModel.VehicleO2){
试一试{
DateFormat格式=新的SimpleDateFormat(“MM-dd-yyyy-hh:MM:ss”);
返回format.parse(o1.getCreated()).compareTo(format.parse(o2.getCreated());
}捕获(例外e){
e、 printStackTrace();
返回0;
}
}
});
customerListAdapter.notifyDataSetChanged();
它不起作用,然后我试着 但此
日期(时间戳)
已被弃用


请帮助使用长时间戳值进行排序

     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 Long.valueOf(format.parse(o1.getCreated()).getTime()).compareTo(format.parse(o2.getCreated()).getTime());

                        } catch (Exception e) {
                            e.printStackTrace();
                            return 0;
                        }
                    }
                });
                customerListAdapter.notifyDataSetChanged();
Collections.sort(车辆、新比较器(){
@凌驾
公共整数比较(VehicleListModel.VehicleO1,VehicleListModel.VehicleO2){
试一试{
DateFormat格式=新的SimpleDateFormat(“MM-dd-yyyy-hh:MM:ss”);
返回Long.valueOf(format.parse(o1.getCreated()).getTime()).compareTo(format.parse(o2.getCreated()).getTime());
}捕获(例外e){
e、 printStackTrace();
返回0;
}
}
});
customerListAdapter.notifyDataSetChanged();

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

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

我已经试过并搜索过了,但它现在起作用了。这还不足以让我们帮助你!怎么不起作用?这里的字符串是什么:getCreated()???getCreated()返回添加到服务的行的时间戳现在我必须根据时间戳排序
getCreated()
的返回类型是什么?日期是可比的,所以这应该不会有太大变化。getCreated()返回添加到服务的行的时间戳现在我必须根据longsir中的时间戳进行排序,getCreated()返回添加到服务的行的时间戳现在我必须根据timestamp@KRoy你是说
java.sql.Timestamp
还是仅仅是一个
long
值?long值@Darshan简单地根据长值排序吗?是的,它们是时间戳,所以您不需要担心其他任何事情(例如日期等),试试这个,如果不起作用,请告诉我。
List<ChainCode> list = new ArrayList<ChainCode>();
list.sort((e1, e2) -> new Long(e1.getCreated()).compareTo(new Long(e2.getCreated())));