I'm iterating over the files in a given directory in ruby, ie:
Dir.each
I'd like to iterate over the files in sorted order - descending or ascending by last edit date. What's the shortest way to write code to do this, in ruby?
I'm iterating over the files in a given directory in ruby, ie:
Dir.each
I'd like to iterate over the files in sorted order - descending or ascending by last edit date. What's the shortest way to write code to do this, in ruby?
This will sort them in ascending order:
Dir['*'].sort_by{|f| File.mtime(f) }
if you want them in descending order, add reverse!
which seems to be the fastest method:
Dir['*'].sort_by{|f| File.mtime(f) }.reverse!