aspose file tools*
The moose likes I/O and Streams and the fly likes listing files in a sorted order Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "listing files in a sorted order" Watch "listing files in a sorted order" New topic
Author

listing files in a sorted order

sri chitra
Greenhorn

Joined: Nov 16, 2000
Posts: 22
The method File[] listFiles() will return back an array of
file objects. But I want to have the array sorted alphabetically.
How can it be done?
James Swan
Ranch Hand

Joined: Jun 26, 2001
Posts: 403
Hi,
you could investigate the java.util.Arrays or java.util.Collections APIs. They have various methods for performing sorting on arrays or lists.
I tried the following code:

When run under windows, the File[] is already sorted for you, but when run under Unix I needed to add the call to Arrays.sort(Object[]).
If the desired sorting isn't what you are after then you will have to define your own custom sorting by implementing Comparator and calling something like Arrays.sort(Object[] myArray, Comparator myComparator).
James.
sri chitra
Greenhorn

Joined: Nov 16, 2000
Posts: 22
Thanks for your reply.
I got the alphabetically sorted list by using Arrays.sort(Object[])
Thanks again
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: listing files in a sorted order
 
Similar Threads
Unable to Create a LoginContext
Tomcat 3.2.3 and VisualAge Java 4
Getting notification when file has changed
help with loop
sort an array