java List Collections 根據時間對List集合內的元素排序
按時間正序排序。
核心程式碼
List<Folderfile> folderfiles = new ArrayList<>();
...往集合裡新增元素此處省略.....
Collections.sort(folderfiles, new Comparator<Folderfile>() {
@Override
public int compare(Folderfile arg0, Folderfile arg1) {
int mark = 1;
try {
Date date0 = arg0.getTime();
Date date1 = arg1.getTime();
if (date0.getTime() > date1.getTime()) {
mark = -1;
}
if (date0.equals(date1)) {
mark = 0;
}
} catch (Exception e) {
e.printStackTrace();
ExceptionUtil.recordErrorMsg(e);
}
return mark;
} // compare
});