FileDateComparator.java
import java.io.*;
import java.util.*;

/**
 * An object to compare 2 <code>java.io.File</code> objects in terms of
 * the last modification date of their corresponding files. Note that the
 * 'natural' ordering as implemented by the <code>java.io.File.compareTo
 * </code> method is based on a lexicographical comparison of filenames,
 * which may not be appropriate in some circumstances.
 *
 * @author Michael Fitzmaurice, February 2003
 */
public class FileDateComparator implements Comparator
{
    public FileDateComparator()
    {

    }

    /**
     * Compares its two arguments for order. Returns a negative integer, zero,
     * or a positive integer as the first argument is less than, equal to,
     * or greater than the second. Objects passes as arguments should be
     * instances of <code>java.io.File</code>. Ordering is based on the last
     * modified date of the files being compared.
     *
     * @param o1 The first object to be compared
     * @param o2 The second object to be compared
     *
     * @return 0 if and only if the files are considered equal according to the
     * <code>File.equals</code> method, a negative integer if <code>o1</code>
     * is less than (i.e. older than) <code>o2</code>, or a positive integer if
     * <code>o1</code> is greater than (newer than) <code>o2</code>
     *
     * @exception ClassCastException if <code>o1</code> and <code>o2</code> are
     * not both instances of <code>java.io.File</code>
     */
    public int compare(Object o1, Object o2)throws ClassCastException
    {
        // ClassCastException will be thrown here if objects are not both Files
        File file1 = (File)o1;
        File file2 = (File)o2;

        int returnValue = 0;

        // only return 0 if the equals method returns true
        if (! file1.equals(file2) )
        {
            long file1Modified   = file1.lastModified();
            long file2Modified   = file2.lastModified();

            if (file1Modified == file2Modified)
            {
                // mod dates are equal - rely on the 'natural' ordering
                returnValue = file1.compareTo(file2);
            }
            else
            {
                if (file1Modified < file2Modified)
                    returnValue = -1;
                else
                    returnValue = 1;
            }
        }

        return returnValue;
    }

    /**
     * Indicates whether some other object is "equal to" this one.
     *
     * @param obj The reference object with which to compare.
     *
     * @return True if this object is the same as the obj argument; false
     * otherwise. A call is made to <code>java.lang.Object.equals()</code> to
     * check for reference equality.
     */
    public boolean equals(Object obj)
    {
        return super.equals(obj);
    }

    // basic unit test - runtime parameter required is the full
    // path to a directory on the local machine whose contents
    // are to be listed in modification date order (oldest file
    // first)
    public static void main(String[] args)
    {
        File directoryToSort  = new File(args[0]);
        File[] filesToSort    = directoryToSort.listFiles();

        // sort into the natural (lexicographically sorted) order first
        Arrays.sort(filesToSort);
        System.out.println( "CONTENTS OF " + directoryToSort + " SORTED BY NAME:\n" +
                            ( Arrays.asList(filesToSort) ) );

        // now sort (hopefully) according to mod date
        Arrays.sort( filesToSort, new FileDateComparator() );
        System.out.println("---------------------------------------------------");
        System.out.println( "CONTENTS OF " + directoryToSort + " SORTED BY MOD DATE:\n" +
                            ( Arrays.asList(filesToSort) ) );
    }
}

FileDateComparator.java