Sort order in `Dir.entries`

前端 未结 4 2045
逝去的感伤
逝去的感伤 2020-12-03 07:33

Is there a fixed/default sort order in which Dir.entries returns results? I know by experience that the first two entries are \".\" and \"..\

4条回答
  •  伪装坚强ぢ
    2020-12-03 08:10

    According to the Ruby language docs, Dir.entries() does not guarantee any particular order of the listed files, so if you require some order it's best to do it explicitly yourself.

    For example, if you need to sort by file modification time (oldest to newest), you could do the following:

    Dir.entries('.').sort_by { |x| File.mtime(x) }
    

提交回复
热议问题