File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes Question about FilenameFilter Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Question about FilenameFilter" Watch "Question about FilenameFilter" New topic

Question about FilenameFilter

kartik krishnan
Ranch Hand

Joined: Nov 19, 2006
Posts: 63

I am using FilenameFilter to return an array of pdf files.

When I use a File#listFile(new FilenameFilter() {
public boolean accept(File file, String name) {
return name.endwith(".pdf");

I get an array of File objects. I wanted to know if there is a way to basically manage the order they appear in an array. For example, if i have files a.pdf, b.pdf and c.pdf, is there a way to return an array of files in this order [b.pdf, c.pdf and a.pdf] if this is not the order that the FilenameFilter returns the file objects in.
Stevi Deter
Ranch Hand

Joined: Mar 22, 2008
Posts: 265


FilenameFilter only determines if a file should be in the list.

Per the API, File#listFiles(FilenameFilter) behaves the same as listFiles(), only it only returns those abstract pathnames that match the filter.

File#listFiles() explicitly states there is no guarantee of the order that files will be returned.

If you need the files in a specific order, you'll have to write your own method for manipulating the File[] array returned by listFiles(FilenameFilter).
[ April 24, 2008: Message edited by: Stevi Deter ]

There will always be people who are ahead of the curve, and people who are behind the curve. But knowledge moves the curve. --Bill James
Jim Yingst

Joined: Jan 30, 2000
Posts: 18671
Arrays.sort() would probably be a good starting point for that...
[ April 24, 2008: Message edited by: Jim Yingst ]

"I'm not back." - Bill Harding, Twister
Rob Spoor

Joined: Oct 27, 2005
Posts: 20276

For which you need a comparator if alphabetical sorting is not what you need.

How To Ask Questions How To Answer Questions
I agree. Here's the link:
subject: Question about FilenameFilter
It's not a secret anymore!