Is there a natural_sort_by method for Ruby?

前端 未结 7 1144
说谎
说谎 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 06:00

    It is sorting correctly. The problem here is that the names aren't good to sort the way you want. In means of string, 10 comes before 2 and 21 comes before 5.

    If you want it to sort it like it was numbers, you have 2 approaches:

    1 - Change all your listings to add a leading 0 before numbers with just one digit.

    2 - Do as William suggested, aplit the name, transform the string to integer and sort by it.

    I would recommend option 1 since the second rely on the padronization of the names.

    0 讨论(0)
提交回复
热议问题