Java: Sort Files By Date Modified In Descending Order

There came a point that I had to display a list of log files in a certain directory for my boss to view. dirThe important thing with the list display is that the files should be sorted in descending order. From top would be the latest while the oldest would be down at the bottom. Java can do sorting through the Comparator Class. And luckily, I found a post in a forum that saved me the time and trouble to make a custom method for it. The code looks like this.

Take note that this method includes sorting directories together with the files as one.

Related Posts Plugin for WordPress, Blogger...


  1. Arrays.sort(fileList, new Comparator() {
    public int compare(File f1, File f2) {
    return Long.valueOf(f1.lastModified()).compareTo(

Leave a Reply

Your email address will not be published. Required fields are marked *