Login  Register

Re: properly sorted file list in java on Mac

Posted by ctrueden on Nov 21, 2007; 6:12pm
URL: http://imagej.273.s1.nabble.com/properly-sorted-file-list-in-java-on-Mac-tp3697924p3697925.html

Hi Bill,

The listFiles() method of File is not guaranteed to return the files
in any specific order. On my Mac, it actually does return them in
case-insensitive alphabetical order, but that behavior is specifically
documented as not being guaranteed. And judging from your experience,
it's clearly platform-specific.

Anyway, you can sort the resulting file list with Arrays.sort. For example:

File[] list = new File(".").listFiles();
Arrays.sort(list);
for (int i=0; i<list.length; i++) System.out.println(list[i]);

-Curtis

On Nov 21, 2007 11:18 AM, Bill Mohler <[hidden email]> wrote:

> This question is related to a plugin we use in ImageJ, but is more
> directly related to Java on the Mac.
>
> Our program calls up and acts upon a list of alphabetically ordered files
> from a directory.  When run on Windows, it addresses these files in the
> appropriate alphabetical order.  But on Mac, it does them in a somewhat
> random order.  These are the exact same files from the exact same
> directory on our server, in each case.
>
> If we list the files in the directory through a Unix shell on the Mac,
> using the -f option for unsorted list, we get the exact same "random"
> order that our program accesses when running on the Mac.
>
> The question, then, is what the proper Java code should be on the Mac so
> that we get an alphabetically sorted list of files for our program to
> access in order.  And why would the Java code for a file list work right
> on Windows and not on Mac?
>
> Thanks for any advice,
> Bill
>