Is there a natural_sort_by method for Ruby?

前端 未结 7 1173
说谎
说谎 2020-12-06 05:06

I have a list of files with a bunch of attributes. One of the attributes is the file name which is how I would like to sort the list. However, the list goes something like t

7条回答
  •  难免孤独
    2020-12-06 05:52

    As long as files are always named "file #", you could do

    files.sort_by{|f| f.name.split(" ")[1].to_i }

    This splits on the space, and grabs the number to do the sorting.

提交回复
热议问题