Is there a natural_sort_by method for Ruby?

前端 未结 7 1172
说谎
说谎 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:42

    Natural Sort gem.

    Install

    gem "natural_sort"

    Usage

    list = ["a10", "a", "a20", "a1b", "a1a", "a2", "a0", "a1"]
    list.sort(&NaturalSort) # => ["a", "a0", "a1", "a1a", "a1b", "a2", "a10", "a20"]
    

提交回复
热议问题