Sort order in `Dir.entries`

前端 未结 4 2044
逝去的感伤
逝去的感伤 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:15

    For others who may come here with the same doubt. A way to select only the files that match a regular expression and still be able to sort them the way you want is:

    files_sorted_by_date = Dir["your regex"].sort_by { |x| File.birthtime(x) }.reverse

    or

    files_sorted_by_date = Dir["your regex"].sort_by { |x| File.birthtime(x) }

    Depending on how you want your files sortes.

    I couldn't do the same using the Dir.entries method

提交回复
热议问题