How to sort an alphanumeric array in ruby

后端 未结 8 1879
没有蜡笔的小新
没有蜡笔的小新 2021-01-19 16:01

How I can sort array data alphanumerically in ruby?

Suppose my array is a = [test_0_1, test_0_2, test_0_3, test_0_4, test_0_5, test_0_6, test_0_7, test_0_8, te

8条回答
  •  梦谈多话
    2021-01-19 16:46

    From the looks of it, you want to use the sort function and/or the reverse function.

    ruby-1.9.2-p136 :009 > a = ["abc_1", "abc_11", "abc_2", "abc_3", "abc_22"]
     => ["abc_1", "abc_11", "abc_2", "abc_3", "abc_22"] 
    
    ruby-1.9.2-p136 :010 > a.sort
     => ["abc_1", "abc_11", "abc_2", "abc_22", "abc_3"] 
    ruby-1.9.2-p136 :011 > a.sort.reverse
     => ["abc_3", "abc_22", "abc_2", "abc_11", "abc_1"] 
    

提交回复
热议问题