如何在ruby的最后修改时间之前订购文件? [英] How to order files by last modified time in ruby?
本文介绍了如何在ruby的最后修改时间之前订购文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在ruby中以最后修改的时间顺序获取文件?我能够将键盘砸得足够大以达到此目的:
How to get files in last modified time order in ruby? I was able to smash my keyboard enough to achieve this:
file_info = Hash[*Dir.glob("*").collect {|file| [file, File.ctime(file)]}.flatten]
sorted_file_info = file_info.sort_by { |k,v| v}
sorted_files = sorted_file_info.collect { |file, created_at| file }
但是我想知道是否还有更复杂的方法?
But I wonder if there is more sophisticated way to do this?
推荐答案
简单地说:
# If you want 'modified time', oldest first
files_sorted_by_time = Dir['*'].sort_by{ |f| File.mtime(f) }
# If you want 'directory change time' (creation time for Windows)
files_sorted_by_time = Dir['*'].sort_by{ |f| File.ctime(f) }
这篇关于如何在ruby的最后修改时间之前订购文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文