All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.commons.io.comparator.package.html Maven / Gradle / Ivy

Go to download

The Apache Commons IO library contains utility classes, stream implementations, file filters, file comparators, endian transformation classes, and much more.

There is a newer version: 0.63.0.19
Show newest version




This package provides various {@link java.util.Comparator} implementations for {@link java.io.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 {@link java.util.Comparator} implementations have some convenience singleton(thread-safe) instances ready to use:





© 2015 - 2024 Weber Informatics LLC | Privacy Policy