我有一个时间戳(长)列表,我必须根据时间或列表添加列表。
我已经尝试过,但现在正在努力。
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();
请帮助
答案 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();