How do I sort an array of files according to their last modified dates?
Author: Deron Eriksson
Description: This Java tutorial describes how to sort an array of files according to their last-modified date/time using a Comparator from Commons IO.
Tutorial created using:
Windows XP || JDK 1.5.0_09 || Eclipse Web Tools Platform 2.0 (Eclipse 3.3.0)
The LastModifiedFileComparator.LASTMODIFIED_COMPARATOR and LastModifiedFileComparator.LASTMODIFIED_REVERSE Comparator singleton instances in the ApacheSW Commons IOS library can be used to sort arrays or collections of files according to their last-modified dates. The LASTMODIFIED_COMPARATOR Comparator allows for sorting in ascending order while the LASTMODIFIED_REVERSE Comparator allows for sorting in descending order. The LastModifiedFileComparatorTest class demonstrates this. It first displays an array of files in my project's root directory. It sorts the array of files in file-modified date ascending order using LASTMODIFIED_COMPARATOR and then displays the results. Next, it sorts the array of files in file-modified date descending order using LASTMODIFIED_REVERSE and then displays the results. LastModifiedFileComparatorTest.javapackage test; import java.io.File; import java.io.FileFilter; import java.io.IOException; import java.util.Arrays; import java.util.Date; import org.apache.commons.io.comparator.LastModifiedFileComparator; import org.apache.commons.io.filefilter.FileFileFilter; public class LastModifiedFileComparatorTest { public static void main(String[] args) throws IOException { File directory = new File("."); // get just files, not directories File[] files = directory.listFiles((FileFilter) FileFileFilter.FILE); System.out.println("Default order"); displayFiles(files); Arrays.sort(files, LastModifiedFileComparator.LASTMODIFIED_COMPARATOR); System.out.println("\nLast Modified Ascending Order (LASTMODIFIED_COMPARATOR)"); displayFiles(files); Arrays.sort(files, LastModifiedFileComparator.LASTMODIFIED_REVERSE); System.out.println("\nLast Modified Descending Order (LASTMODIFIED_REVERSE)"); displayFiles(files); } public static void displayFiles(File[] files) { for (File file : files) { System.out.printf("File: %-20s Last Modified:" + new Date(file.lastModified()) + "\n", file.getName()); } } } The console output of LastModifiedFileComparatorTest is shown below. ResultsDefault order File: .classpath Last Modified:Sat Jan 26 17:57:58 PST 2008 File: .project Last Modified:Thu Nov 23 21:28:30 PST 2006 File: mytest.txt Last Modified:Sun Jan 27 17:49:57 PST 2008 File: mytest2.txt Last Modified:Sun Jan 27 20:55:58 PST 2008 Last Modified Ascending Order (LASTMODIFIED_COMPARATOR) File: .project Last Modified:Thu Nov 23 21:28:30 PST 2006 File: .classpath Last Modified:Sat Jan 26 17:57:58 PST 2008 File: mytest.txt Last Modified:Sun Jan 27 17:49:57 PST 2008 File: mytest2.txt Last Modified:Sun Jan 27 20:55:58 PST 2008 Last Modified Descending Order (LASTMODIFIED_REVERSE) File: mytest2.txt Last Modified:Sun Jan 27 20:55:58 PST 2008 File: mytest.txt Last Modified:Sun Jan 27 17:49:57 PST 2008 File: .classpath Last Modified:Sat Jan 26 17:57:58 PST 2008 File: .project Last Modified:Thu Nov 23 21:28:30 PST 2006 Related Tutorials:
|