properly sorted file list in java on Mac

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

properly sorted file list in java on Mac

Bill Mohler
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
Reply | Threaded
Open this post in threaded view
|

Re: properly sorted file list in java on Mac

ctrueden
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
>
Reply | Threaded
Open this post in threaded view
|

Re: properly sorted file list in java on Mac

Bill Mohler
Hi Curtis-

Thanks for the clarification/confirmation.  Am I right that your
sorting solution will work equally well on all platforms that run
Java?

Bill

>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
>>
Reply | Threaded
Open this post in threaded view
|

Re: properly sorted file list in java on Mac

Jim Cant
In reply to this post by ctrueden
As you can see from the other response, both Windows and Mac are 'right' as you are only promised a list; that Mac's comes sorted is only fortuitous.

One gotcha to watch for: If you have a numeric component to the file names, they may sort in an unexpeced way.  Files F1, F2,...F10, F11... will sort at F1,F10,F11, F2.  which can be problem if the file set needs to be ordered properly (time series, images of serial sections, etc.).  This can be  avoided by using a fixed width for the numeric portion and padding with '0', i.e.  F01, F02...F11

Curtis Rueden wrote
...snip...
> 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
>