Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我正在遍历 ruby 中给定目录中的文件,即:
Dir.each
我想按排序顺序遍历文件 - 按上次编辑日期降序或升序。在 ruby 中编写代码来做到这一点的最短方法是什么?
这将按升序对它们进行排序:
Dir['*'].sort_by{|f| File.mtime(f) }
如果您希望它们按降序排列,请添加reverse!似乎是最快的方法:
reverse!
Dir['*'].sort_by{|f| File.mtime(f) }.reverse!