Connie Varley
Connie Varley

Reputation: 33

Does java.io.File.listFiles(FilenameFilter filter) already sort the file object?

Good day.

I was wondering if java.io.File.listFiles(FilenameFilter filter) returns an already sorted object.

Here is my code:

String[] files = FIUtil.getFilesList(FIConstants.getIFDirectory(filePrefix), 
FIConstants.VALID_INPUT_FILE_SUFFIX,filePrefix);

   log.debug("=== LOOKING FOR FILES IN ===" + FIConstants.getIFDirectory(filePrefix));
   log.debug("=== Inside directory ===");
   for(int i=0;i<files.length;i++){
       log.debug("=== "+files[i]); }

 public static String[] getFilesList(String directory, final String suffix,String prefix)
   {
      try {
         File fileObject = new File(directory);
         return fileObject.list((new FilenameFilter() {
            @Override
        public boolean accept(File dir, String name) {
                return name.startsWith(prefix) && name.endsWith(suffix);        
        }
    }));
  }
  catch (SecurityException se) {
  }
  return null;
} 

My files in the directory are not sorted.
But when I check the listing of files in my logs, they are already sorted.

Upvotes: 2

Views: 1032

Answers (2)

Karol Dowbecki
Karol Dowbecki

Reputation: 44962

As explained in File.listFiles() Javadoc there is no order guranteed

There is no guarantee that the name strings in the resulting array will appear in any specific order; they are not, in particular, guaranteed to appear in alphabetical order.

Upvotes: 3

Thomas S.
Thomas S.

Reputation: 6345

No, it does not sort by name, date or anything.

Upvotes: 2

Related Questions