You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Java中如何按创建日期对File.listFiles()结果排序?

Hey there! Let's get your file sorting by creation date sorted out without relying on Apache Commons. Your current code just reverses the default alphabetical sort, so we need to replace that with a comparator that looks at the file's date attributes instead.

Traditional IO Approach (Java 6+)

If you're working with Java 6 or older, or you can accept using the file's last modified time (note: this behaves differently across OSes—Windows uses creation time here, while Linux/macOS uses last modified), use this:

File[] files = reportFolder.listFiles(new ReportFolderFilter());

// Sort files by date (ascending: oldest first)
Arrays.sort(files, new Comparator<File>() {
    @Override
    public int compare(File file1, File file2) {
        return Long.compare(file1.lastModified(), file2.lastModified());
    }
});

// For descending order (newest first), reverse the comparison:
// Arrays.sort(files, (file1, file2) -> Long.compare(file2.lastModified(), file1.lastModified()));
// ^ That lambda works in Java 8+; for Java 6/7, swap the order in the Long.compare call

Strict Creation Date with NIO (Java 7+)

If you need the actual creation date (not last modified) across all platforms, use Java 7's NIO API—this is more reliable for cross-system behavior:

import java.nio.file.Files;
import java.nio.file.attribute.FileTime;
import java.util.Arrays;
import java.util.Comparator;

// ...

File[] files = reportFolder.listFiles(new ReportFolderFilter());

Arrays.sort(files, new Comparator<File>() {
    @Override
    public int compare(File file1, File file2) {
        try {
            // Get creation time attribute from each file
            FileTime creationTime1 = (FileTime) Files.getAttribute(file1.toPath(), "creationTime");
            FileTime creationTime2 = (FileTime) Files.getAttribute(file2.toPath(), "creationTime");
            // Compare creation times (ascending order)
            return creationTime1.compareTo(creationTime2);
        } catch (Exception e) {
            // Fall back to last modified if creation time isn't available
            return Long.compare(file1.lastModified(), file2.lastModified());
        }
    }
});

// Java 8+ can simplify this with a lambda:
Arrays.sort(files, (file1, file2) -> {
    try {
        FileTime ct1 = (FileTime) Files.getAttribute(file1.toPath(), "creationTime");
        FileTime ct2 = (FileTime) Files.getAttribute(file2.toPath(), "creationTime");
        return ct1.compareTo(ct2);
    } catch (Exception e) {
        return Long.compare(file1.lastModified(), file2.lastModified());
    }
});

Quick Notes

  • To get newest first (descending order), just reverse the comparison result—e.g., return creationTime2.compareTo(creationTime1); instead of the ascending version.
  • The NIO approach handles edge cases better: some file systems don't track creation time, so the fallback to lastModified() ensures your code still works.
  • Don't forget to import the necessary classes (like java.util.Arrays and java.util.Comparator) if they aren't already in your code.

内容的提问来源于stack exchange,提问作者Paul Taylor

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.22 07:50:34