| Author |
Question about FilenameFilter
|
kartik krishnan
Ranch Hand
Joined: Nov 19, 2006
Posts: 63
|
|
Hi, 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
|
|
Kartik, 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
Wanderer
Sheriff
Joined: Jan 30, 2000
Posts: 18670
|
|
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
Sheriff
Joined: Oct 27, 2005
Posts: 19216
|
|
|
For which you need a comparator if alphabetical sorting is not what you need.
|
SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
|
 |
 |
|
|
subject: Question about FilenameFilter
|
|
|