如何排序时间戳列表?

时间:2017-05-29 09:02:48

标签: java android sorting date timestamp

我有一个时间戳(长)列表,我必须根据时间或列表添加列表。

我已经尝试过,但现在正在努力。

android {
  …
  splits { 
    abi {
      enable true
      reset()
      include "armeabi-v7a"
      universalApk false
    }
  }
}

它不工作然后我试了 this但不推荐使用此 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();

请帮助

2 个答案:

答案 0 :(得分:3)

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

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

<强>更新

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

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

答案 1 :(得分:0)

使用长时间戳值进行评分

     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();