file list sorted by creation date

Discussion in 'Ruby' started by Rajarshi Chakravarty, Jan 8, 2011.

  1. Hi,
    I need to get a list of all files in a directory sorted by their
    date/time of creation.
    Please help.

    --
    Posted via http://www.ruby-forum.com/.
     
    Rajarshi Chakravarty, Jan 8, 2011
    #1
    1. Advertising

  2. Anurag Priyam, Jan 8, 2011
    #2
    1. Advertising

  3. Rajarshi Chakravarty

    zuerrong Guest

    2011/1/8 Rajarshi Chakravarty <>:
    > Hi,
    > I need to get a list of all files in a directory sorted by their
    > date/time of creation.


    sort_by it:

    Dir.entries(".").sort_by{|c| File.stat(c).ctime}
     
    zuerrong, Jan 8, 2011
    #3
  4. Rajarshi Chakravarty

    zuerrong Guest

    Or use a Swatz* Transform which is used by sort_by inherently:

    Dir.entries(".").map{|c| [c,File.stat(c).ctime]}.sort{|a,b|
    a[1]<=>b[1]}.map{|d| d[0]}

    * Swatz is a Perl hacker whose full name is Randal Swatz (hope me
    remember that correctly).

    2011/1/8 zuerrong <>:
    > 2011/1/8 Rajarshi Chakravarty <>:
    >> Hi,
    >> I need to get a list of all files in a directory sorted by their
    >> date/time of creation.

    >
    > sort_by it:
    >
    > Dir.entries(".").sort_by{|c| File.stat(c).ctime}
    >
     
    zuerrong, Jan 8, 2011
    #4
  5. Rajarshi Chakravarty, Jan 8, 2011
    #5
  6. On Sat, Jan 8, 2011 at 6:45 PM, zuerrong <> wrote:
    > Or use a Swatz* Transform which is used by sort_by inherently:
    >
    > Dir.entries(".").map{|c| [c,File.stat(c).ctime]}.sort{|a,b|
    > a[1]<=>b[1]}.map{|d| d[0]}
    >
    > * Swatz is a Perl hacker whose full name is Randal Swatz (hope me
    > remember that correctly).


    You mean Randal Schwartz and his Schwartzian transform:
    https://secure.wikimedia.org/wikipedia/en/wiki/Schwartzian_transform

    As you said, sort_by implements that already, so it's faster to use it
    instead directly.

    >
    > 2011/1/8 zuerrong <>:
    >> 2011/1/8 Rajarshi Chakravarty <>:
    >>> Hi,
    >>> I need to get a list of all files in a directory sorted by their
    >>> date/time of creation.

    >>
    >> sort_by it:
    >>
    >> Dir.entries(".").sort_by{|c| File.stat(c).ctime}




    --
    Michael Fellinger
    CTO, The Rubyists, LLC
     
    Michael Fellinger, Jan 8, 2011
    #6
    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. Matt
    Replies:
    1
    Views:
    651
    Matthew Speed
    Nov 8, 2003
  2. Peter Grison

    Date, date date date....

    Peter Grison, May 28, 2004, in forum: Java
    Replies:
    10
    Views:
    3,379
    Michael Borgwardt
    May 30, 2004
  3. Replies:
    2
    Views:
    521
    Gerard Flanagan
    Mar 22, 2006
  4. Santiago  Romero
    Replies:
    10
    Views:
    541
    Peter Otten
    Jan 21, 2008
  5. Yash
    Replies:
    5
    Views:
    127
    John W. Krahn
    May 29, 2004
Loading...

Share This Page