How to order files by last modified time in ruby?

后端 未结 3 1867
囚心锁ツ
囚心锁ツ 2020-12-14 15:35

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 {|fil         


        
3条回答
  •  [愿得一人]
    2020-12-14 16:31

    How about simply:

    # 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) }
    

提交回复
热议问题