Is there a natural_sort_by method for Ruby?

前端 未结 7 1165
说谎
说谎 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:53

    generic answer for strings natural sort

    array.sort_by {|e| e.split(/(\d+)/).map {|a| a =~ /\d+/ ? a.to_i : a }}
    

提交回复
热议问题