file list sorted by creation date

  • Thread starter Rajarshi Chakravarty
  • Start date
R

Rajarshi Chakravarty

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

zuerrong

2011/1/8 Rajarshi Chakravarty said:
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}
 
Z

zuerrong

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).
 
M

Michael Fellinger

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top