Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Java (http://www.velocityreviews.com/forums/f30-java.html)
-   -   Sorting a directory content by last modify date (http://www.velocityreviews.com/forums/t390432-sorting-a-directory-content-by-last-modify-date.html)

J_Zanetti 01-29-2007 11:12 AM

Sorting a directory content by last modify date
 
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 01-29-2007 11:39 AM

Re: Sorting a directory content by last modify date
 
On Jan 29, 10:12 pm, "J_Zanetti" <tot...@gmail.com> wrote:
...
> 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 01-29-2007 12:53 PM

Re: Sorting a directory content by last modify date
 


On Jan 29, 12:39 pm, "Andrew Thompson" <andrewtho...@gmail.com> wrote:
> On Jan 29, 10:12 pm, "J_Zanetti" <tot...@gmail.com> wrote:
> ..
>
> > 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.
>

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 01-29-2007 01:18 PM

Re: Sorting a directory content by last modify date
 
On 29 Gen, 13:53, "voorth" <voo...@xs4all.nl> wrote:
>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 01-29-2007 02:38 PM

Re: Sorting a directory content by last modify date
 
J_Zanetti wrote:
> 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 :)
>


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


All times are GMT. The time now is 01:07 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.