Package org.apache.commons.io.comparator
This package provides various Comparator
implementations
for File
s.
Sorting
All the compartors include convenience utility sort(File...)
and
sort(List)
methods.
For example, to sort the files in a directory by name:
File[] files = dir.listFiles(); NameFileComparator.NAME_COMPARATOR.sort(files);
...alternatively you can do this in one line:
File[] files = NameFileComparator.NAME_COMPARATOR.sort(dir.listFiles());
Composite Comparator
The CompositeFileComparator can be used to compare (and sort lists or arrays of files) by combining a number other comparators.
For example, to sort an array of files by type (i.e. directory or file) and then by name:
CompositeFileComparator comparator = new CompositeFileComparator( DirectoryFileComparator.DIRECTORY_COMPARATOR, NameFileComparator.NAME_COMPARATOR); File[] files = dir.listFiles(); comparator.sort(files);
Singleton Instances (thread-safe)
The Comparator
implementations have some convenience
singleton(thread-safe) instances ready to use:
- DefaultFileComparator - default file compare:
- DEFAULT_COMPARATOR
- Compare using
File.compareTo(File)
method. - DEFAULT_REVERSE
- Reverse compare of
File.compareTo(File)
method.
- DEFAULT_COMPARATOR
- Compare using
- DirectoryFileComparator - compare by type (directory or file):
- DIRECTORY_COMPARATOR
- Compare using
File.isDirectory()
method (directories < files). - DIRECTORY_REVERSE
- Reverse compare of
File.isDirectory()
method (directories >files).
- DIRECTORY_COMPARATOR
- Compare using
- ExtensionFileComparator - compare file extenstions:
- EXTENSION_COMPARATOR
- Compare using
FilenameUtils.getExtension(String)
method. - EXTENSION_REVERSE
- Reverse compare of
FilenameUtils.getExtension(String)
method. - EXTENSION_INSENSITIVE_COMPARATOR
- Case-insensitive compare using
FilenameUtils.getExtension(String)
method. - EXTENSION_INSENSITIVE_REVERSE
- Reverse case-insensitive compare of
FilenameUtils.getExtension(String)
method. - EXTENSION_SYSTEM_COMPARATOR
- System sensitive compare using
FilenameUtils.getExtension(String)
method. - EXTENSION_SYSTEM_REVERSE
- Reverse system sensitive compare of
FilenameUtils.getExtension(String)
method.
- EXTENSION_COMPARATOR
- Compare using
- LastModifiedFileComparator
- compare the file's last modified date/time:
- LASTMODIFIED_COMPARATOR
- Compare using
File.lastModified()
method. - LASTMODIFIED_REVERSE
- Reverse compare of
File.lastModified()
method.
- LASTMODIFIED_COMPARATOR
- Compare using
- NameFileComparator - compare file names:
- NAME_COMPARATOR
- Compare using
File.getName()
method. - NAME_REVERSE
- Reverse compare of
File.getName()
method. - NAME_INSENSITIVE_COMPARATOR
- Case-insensitive compare using
File.getName()
method. - NAME_INSENSITIVE_REVERSE
- Reverse case-insensitive compare of
File.getName()
method. - NAME_SYSTEM_COMPARATOR
- System sensitive compare using
File.getName()
method. - NAME_SYSTEM_REVERSE
- Reverse system sensitive compare of
File.getName()
method.
- NAME_COMPARATOR
- Compare using
- PathFileComparator - compare file paths:
- PATH_COMPARATOR
- Compare using
File.getPath()
method. - PATH_REVERSE
- Reverse compare of
File.getPath()
method. - PATH_INSENSITIVE_COMPARATOR
- Case-insensitive compare using
File.getPath()
method. - PATH_INSENSITIVE_REVERSE
- Reverse case-insensitive compare of
File.getPath()
method. - PATH_SYSTEM_COMPARATOR
- System sensitive compare using
File.getPath()
method. - PATH_SYSTEM_REVERSE
- Reverse system sensitive compare of
File.getPath()
method.
- PATH_COMPARATOR
- Compare using
- SizeFileComparator - compare the file's size:
- SIZE_COMPARATOR
- Compare using
File.length()
method (directories treated as zero length). - LASTMODIFIED_REVERSE
- Reverse compare of
File.length()
method (directories treated as zero length). - SIZE_SUMDIR_COMPARATOR
- Compare using
FileUtils.sizeOfDirectory(File)
method (sums the size of a directory's contents). - SIZE_SUMDIR_REVERSE
- Reverse compare of
FileUtils.sizeOfDirectory(File)
method (sums the size of a directory's contents).
- SIZE_COMPARATOR
- Compare using
-
Class Summary Class Description CompositeFileComparator Compare two files using a set of delegate fileComparator
.DefaultFileComparator Compare two files using the defaultFile.compareTo(File)
method.DirectoryFileComparator Compare two files using theFile.isDirectory()
method.ExtensionFileComparator Compare the file name extensions for order (seeFilenameUtils.getExtension(String)
).LastModifiedFileComparator Compare the last modified date/time of two files for order (seeFileUtils.lastModifiedUnchecked(File)
).NameFileComparator Compare the names of two files for order (seeFile.getName()
).PathFileComparator Compare the path of two files for order (seeFile.getPath()
).SizeFileComparator Compare the length/size of two files for order (seeFile.length()
andFileUtils.sizeOfDirectory(File)
).