Q:

Java array sort date modifed

I have a directory with multiple files. I want to sort the files by the date modified difference. 
E.g. 
file1.csv  (created 14:00:55)
file2.csv  (created 14:02:55)
file3.csv  (created 15:22:55)
file4.csv  (created 15:25:55)
file5.csv  (created 16:28:55)

Compare file1 with a set time e.g. 13:59 (file1= 1 minute diference), file1 with file2 (file2 = 2 minute difference), file2 with file3(file 3 = 1 hour and 20 minute difference), file3 with file4(file 4 = 3 minute difference), file4 with file5(file 5 = 1 hour difference). And list them in accordance to the difference. The list should now be :
file3.csv  (created 15:22:55)
file5.csv  (created 14:28:55)
file4.csv  (created 15:25:55)
file1.csv  (created 14:00:55)
file2.csv  (created 15:02:55)

A:

The trick to this is to encapsulate the file (name and actual modification time) in an object along with the value to be used for sorting the list.  I.e., you do this:

Read the file entries in the order in which you want to calculate the differences (file name or modification time)
Load them into objects in a collection and calculate the value by which you want to sort them; set the sort value into the object.
Sort the collection based on the calculated value

With your data, assuming the original order is by filename and start time is 13:59:00
Original                        Calculated
Data                            Sort Key
-----------------------------   ----------
file1.csv  (created 14:00:55)      1:55
file2.csv  (created 14:02:55)      1:00
file3.csv  (created 15:22:55)     20:00
file4.csv  (created 15:25:55)      5:00
file5.csv  (created 16:28:55)      3:00

Sorting by the "Calculated Sort Key" column (descending) gives you what you want.  
Here's a sample in Java (untested):
class FileWithTimeOffset implements Comparable<FileWithTimeOffset>
{
    String fileName;
    Date   creationDate;
    int    timeOffset;
    public int compareTo(FileWithTimeOffset other)
    {
        // return negative of comparison to achieve descending sort
        return -Integer.compare(this.timeOffset,other.timeOffset);
    }
}

