java List Collections 根据时间对List集合内的元素排序

按时间正序排序。

核心代码

List folderfiles = new ArrayList<>();

...往集合里添加元素此处省略.....

Collections.sort(folderfiles, new Comparator() {
                    @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
                });

你可能感兴趣的:(java)