<p>I want to get a list of files in a directory, but I want to sort it such that the oldest files are first.  My solution was to call File.listFiles and just resort the list based on File.lastModified, but I was wondering if there was a better way.</p>

<p>Edit: My current solution, as suggested, is to use an anonymous Comparator:</p>

<pre><code>File[] files = directory.listFiles();

Arrays.sort(files, new Comparator&lt;File&gt;(){
    public int compare(File f1, File f2)
    {
        return Long.valueOf(f1.lastModified()).compareTo(f2.lastModified());
    } });
</code></pre>

I think your solution is the only sensible way.  The only way to get the list of files is to use
File.listFiles()
and the documentation states that this makes no guarantees about the order of the files returned.  Therefore you need to write a
Comparator
that uses
File.lastModified()
and pass this, along with the array of files, to
Arrays.sort()
.