You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@commons.apache.org by "Niall Pemberton (JIRA)" <ji...@apache.org> on 2008/11/29 06:20:44 UTC

[jira] Commented: (IO-176) Add an overridable sort callback to the filenames

    [ https://issues.apache.org/jira/browse/IO-176?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12651696#action_12651696 ] 

Niall Pemberton commented on IO-176:
------------------------------------

btw Commons IO has had a set of Comparator implementations[1] for files since version 1.4 - including a file name implementation that can do case-insensitive compares[2]:

[1] http://commons.apache.org/io/api-release/org/apache/commons/io/comparator/package-summary.html
[2] http://commons.apache.org/io/api-release/org/apache/commons/io/comparator/NameFileComparator.html#NAME_INSENSITIVE_COMPARATOR

I also just added sort methods to those implementations (IO-142) so you will be able to do what you want with the following in DirectoryWalker:

    protected File[] filterDirectoryContents(File directory, int depth, File[] files) throws IOException {
        return NameFileComparator.NAME_INSENSITIVE_COMPARATOR.sort(files);
    }

> 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
>            Assignee: Niall Pemberton
>            Priority: Minor
>             Fix For: 2.0
>
>
> 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.