Mark Szymanski
Mark Szymanski

Reputation: 58120

Create array of files and sort by date in ruby

I have an array of filenames, is there any way to sort these files by modification date?

Upvotes: 7

Views: 7081

Answers (1)

sepp2k
sepp2k

Reputation: 370367

You can use the sort_by method in conjunction with File.mtime method, which returns the last modification time of the given file.

filenames.sort_by {|filename| File.mtime(filename) }

Upvotes: 16

Related Questions