Add an overridable sort  callback to the filenames
--------------------------------------------------

                 Key: IO-176
                 URL: https://issues.apache.org/jira/browse/IO-176
             Project: Commons IO
          Issue Type: Improvement
         Environment: Windows (others too I'm sure)
            Reporter: David Felsenthal
            Priority: Minor


Several people have requested the ability to sort the file names that are used 
by the directory walker. I'd suggest (and have done for myself) using an 
overridable callback rather than a filter. This allows current code to operate 
unbroken.

I changed line 394 of DirectoryWalker:
        
                        File[] childFiles = handleFileNames((filter == null ? 
directory.listFiles() : directory.listFiles(filter)));

Added:
        /**
         * Overridable callback method invoked when a directory is visited.
         * <p>
         * This implementation does nothing.
         * 
         * @param names
         *            the array of File objects in arbitrary order
         *            
         * @return the (possibly manipulated) array of File           
         * @throws IOException
         *             if an I/O Error occurs
         *             
         *             
         */
        protected File[] handleFileNames(File [] names) throws IOException {
                // do nothing - overridable by subclass
                return names;
        }


And overrode it like this in my case, as I wanted reverse alphabetic:

                        @Override
                        protected File[] handleFileNames(File[] names) throws 
IOException {
                                Arrays.sort(names, new Comparator<File>() {
                                        public int compare(File file1, File 
file2) {
                                                return 
file2.getName().toUpperCase().compareTo(
                                                                
file1.getName().toUpperCase());
                                        }
                                });
                                return names;
                        }


-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.

Reply via email to