Class DirectoryFileComparator

  • All Implemented Interfaces:
    java.io.Serializable, java.util.Comparator<java.io.File>

    public class DirectoryFileComparator
    extends java.lang.Object
    implements java.io.Serializable
    Compare two files using the File.isDirectory() method.

    This comparator can be used to sort lists or arrays by directories and files.

    Example of sorting a list of files/directories using the DIRECTORY_COMPARATOR singleton instance:

           List<File> list = ...
           ((AbstractFileComparator) DirectoryFileComparator.DIRECTORY_COMPARATOR).sort(list);
     

    Example of doing a reverse sort of an array of files/directories using the DIRECTORY_REVERSE singleton instance:

           File[] array = ...
           ((AbstractFileComparator) DirectoryFileComparator.DIRECTORY_REVERSE).sort(array);
     

    Since:
    2.0
    See Also:
    Serialized Form
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static java.util.Comparator<java.io.File> DIRECTORY_COMPARATOR
      Singleton default comparator instance
      static java.util.Comparator<java.io.File> DIRECTORY_REVERSE
      Singleton reverse default comparator instance
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      int compare​(java.io.File file1, java.io.File file2)
      Compare the two files using the File.isDirectory() method.
      java.io.File[] sort​(java.io.File... files)
      Sort an array of files.
      java.util.List<java.io.File> sort​(java.util.List<java.io.File> files)
      Sort a List of files.
      java.lang.String toString()
      String representation of this file comparator.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
      • Methods inherited from interface java.util.Comparator

        equals, reversed, thenComparing, thenComparing, thenComparing, thenComparingDouble, thenComparingInt, thenComparingLong
    • Field Detail

      • DIRECTORY_COMPARATOR

        public static final java.util.Comparator<java.io.File> DIRECTORY_COMPARATOR
        Singleton default comparator instance
      • DIRECTORY_REVERSE

        public static final java.util.Comparator<java.io.File> DIRECTORY_REVERSE
        Singleton reverse default comparator instance
    • Constructor Detail

      • DirectoryFileComparator

        public DirectoryFileComparator()
    • Method Detail

      • compare

        public int compare​(java.io.File file1,
                           java.io.File file2)
        Compare the two files using the File.isDirectory() method.
        Specified by:
        compare in interface java.util.Comparator<java.io.File>
        Parameters:
        file1 - The first file to compare
        file2 - The second file to compare
        Returns:
        the result of calling file1's File.compareTo(File) with file2 as the parameter.
      • sort

        public java.io.File[] sort​(java.io.File... files)
        Sort an array of files.

        This method uses Arrays.sort(Object[], Comparator) and returns the original array.

        Parameters:
        files - The files to sort, may be null
        Returns:
        The sorted array
        Since:
        2.0
      • sort

        public java.util.List<java.io.File> sort​(java.util.List<java.io.File> files)
        Sort a List of files.

        This method uses Collections.sort(List, Comparator) and returns the original list.

        Parameters:
        files - The files to sort, may be null
        Returns:
        The sorted list
        Since:
        2.0
      • toString

        public java.lang.String toString()
        String representation of this file comparator.
        Overrides:
        toString in class java.lang.Object
        Returns:
        String representation of this file comparator