Sorting a directory content by last modify date

Discussion in 'Java' started by J_Zanetti, Jan 29, 2007.

  1. J_Zanetti

    J_Zanetti Guest

    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 :)
     
    J_Zanetti, Jan 29, 2007
    #1
    1. Advertising

  2. On Jan 29, 10:12 pm, "J_Zanetti" <> 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.
     
    Andrew Thompson, Jan 29, 2007
    #2
    1. Advertising

  3. J_Zanetti

    voorth Guest

    On Jan 29, 12:39 pm, "Andrew Thompson" <> wrote:
    > On Jan 29, 10:12 pm, "J_Zanetti" <> 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
     
    voorth, Jan 29, 2007
    #3
  4. J_Zanetti

    J_Zanetti Guest

    On 29 Gen, 13:53, "voorth" <> 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 :)
     
    J_Zanetti, Jan 29, 2007
    #4
  5. 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
     
    Patricia Shanahan, Jan 29, 2007
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Peter Grison

    Date, date date date....

    Peter Grison, May 28, 2004, in forum: Java
    Replies:
    10
    Views:
    3,301
    Michael Borgwardt
    May 30, 2004
  2. hazz
    Replies:
    6
    Views:
    49,790
    SkyUCHC
    Jun 9, 2010
  3. Roedy Green

    Date last Accessed vs Date Modified

    Roedy Green, Feb 21, 2008, in forum: Java
    Replies:
    1
    Views:
    1,484
    Nigel Wade
    Feb 22, 2008
  4. Elliot
    Replies:
    1
    Views:
    322
    siccolo
    Feb 27, 2008
  5. Elliot
    Replies:
    3
    Views:
    369
    Elliot
    Jun 16, 2008
Loading...

Share This Page