Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / January 2007

Tip: Looking for answers? Try searching our database.

Sorting a directory content by last modify date

Thread view: 
J_Zanetti - 29 Jan 2007 11:12 GMT
Hello everyone,

Im trying to sort the content of a directory (a file list) according
to the lastModified attribute of the object.
I get the array of files in this way:

File file = new File(directoryname);
String listoffile[] = file.list();

I would like that this listoffile[] array contains the files sorted by
its lastModified date...
is there any way to do that?

thanks everyone who can help me :)
Andrew Thompson - 29 Jan 2007 11:39 GMT
..
> Im trying to sort the content of a directory (a file list) according
> to the lastModified attribute of the object.
...
> I would like that this listoffile[] array contains the files sorted by
> its lastModified date...
> is there any way to do that?

Create a suitable java.util.Comparator.  Read the
JDocs for same, to see some ways to sort them.

HTH

Andrew T.
voorth - 29 Jan 2007 12:53 GMT
> ..
>
[quoted text clipped - 5 lines]
> > is there any way to do that?Create a suitable java.util.Comparator.  Read the
> JDocs for same, to see some ways to sort them.

It might also be a good idea to use File.listFiles() instead of
File.list(). Processing a File array instead of a String array will
make your Comparator a lot simpler...

Henk van Voorthuijsen
J_Zanetti - 29 Jan 2007 13:18 GMT
>It might also be a good idea to use File.listFiles() instead of
> File.list(). Processing a File array instead of a String array will
> make your Comparator a lot simpler...

Thank to both of you that answered...I used comparator with array of
files and it works greatly ;)
Thank so much :)
Patricia Shanahan - 29 Jan 2007 14:38 GMT
> Hello everyone,
>
[quoted text clipped - 10 lines]
>
> thanks everyone who can help me :)

I think you are making a mistake in using file.list directly. You would
do better to use file.listFiles. You could sort the resulting File[]
according to lastModified() result, and then extract the names in the
right order.

Patricia


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.