How do I sort an array of files according to their names?
Author: Deron Eriksson
Description: This Java tutorial describes how to sort an array of files according to their names 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 NameFileComparator class in the ApacheSW Commons IOS library features several Comparator singletons for sorting files by name. Files can be sorted in ascending and descending order, with case sensitivity or case insensitivity. The NameFileComparatorTest class demonstrates this.

NameFileComparatorTest.java

package 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.NameFileComparator;
import org.apache.commons.io.filefilter.FileFileFilter;

public class NameFileComparatorTest {

	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, NameFileComparator.NAME_COMPARATOR);
		System.out.println("\nNames, case sensitive ascending order (NAME_COMPARATOR)");
		displayFiles(files);

		Arrays.sort(files, NameFileComparator.NAME_INSENSITIVE_COMPARATOR);
		System.out.println("\nNames, case insensitive ascending order (NAME_INSENSITIVE_COMPARATOR)");
		displayFiles(files);

		Arrays.sort(files, NameFileComparator.NAME_REVERSE);
		System.out.println("\nNames, case sensitive descending order (NAME_REVERSE)");
		displayFiles(files);

		Arrays.sort(files, NameFileComparator.NAME_INSENSITIVE_REVERSE);
		System.out.println("\nNames, case insensitive descending order (NAME_INSENSITIVE_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 NameFileComparatorTest class gets an array of files from my project's root directory, excluding directories. It displays the array of files in their default order. After this, it uses various comparators to sort the file names in ascending and descending order, with case sensitivity and case insensitivity. The results are shown below:

Results

Default 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: Test1.txt            Last Modified:Sun Jan 27 17:49:57 PST 2008
File: test2.txt            Last Modified:Sun Jan 27 20:55:58 PST 2008
File: Test3.txt            Last Modified:Sun Jan 27 17:49:57 PST 2008
File: test4.txt            Last Modified:Sun Jan 27 20:55:58 PST 2008

Names, case sensitive ascending order (NAME_COMPARATOR)
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: Test1.txt            Last Modified:Sun Jan 27 17:49:57 PST 2008
File: Test3.txt            Last Modified:Sun Jan 27 17:49:57 PST 2008
File: test2.txt            Last Modified:Sun Jan 27 20:55:58 PST 2008
File: test4.txt            Last Modified:Sun Jan 27 20:55:58 PST 2008

Names, case insensitive ascending order (NAME_INSENSITIVE_COMPARATOR)
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: Test1.txt            Last Modified:Sun Jan 27 17:49:57 PST 2008
File: test2.txt            Last Modified:Sun Jan 27 20:55:58 PST 2008
File: Test3.txt            Last Modified:Sun Jan 27 17:49:57 PST 2008
File: test4.txt            Last Modified:Sun Jan 27 20:55:58 PST 2008

Names, case sensitive descending order (NAME_REVERSE)
File: test4.txt            Last Modified:Sun Jan 27 20:55:58 PST 2008
File: test2.txt            Last Modified:Sun Jan 27 20:55:58 PST 2008
File: Test3.txt            Last Modified:Sun Jan 27 17:49:57 PST 2008
File: Test1.txt            Last Modified:Sun Jan 27 17:49:57 PST 2008
File: .project             Last Modified:Thu Nov 23 21:28:30 PST 2006
File: .classpath           Last Modified:Sat Jan 26 17:57:58 PST 2008

Names, case insensitive descending order (NAME_INSENSITIVE_REVERSE)
File: test4.txt            Last Modified:Sun Jan 27 20:55:58 PST 2008
File: Test3.txt            Last Modified:Sun Jan 27 17:49:57 PST 2008
File: test2.txt            Last Modified:Sun Jan 27 20:55:58 PST 2008
File: Test1.txt            Last Modified:Sun Jan 27 17:49:57 PST 2008
File: .project             Last Modified:Thu Nov 23 21:28:30 PST 2006
File: .classpath           Last Modified:Sat Jan 26 17:57:58 PST 2008