This week's book giveaway is in the Design forum.
We're giving away four copies of Building Microservices and have Sam Newman on-line!
See this thread for details.
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

Win a copy of Building Microservices this week in the Design forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "listing files in a sorted order" Watch "listing files in a sorted order" New topic

listing files in a sorted order

sri chitra

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
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).
sri chitra

Joined: Nov 16, 2000
Posts: 22
Thanks for your reply.
I got the alphabetically sorted list by using Arrays.sort(Object[])
Thanks again
I’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link:
subject: listing files in a sorted order
It's not a secret anymore!