Sort order in `Dir.entries`

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

Leave a Comment