Ruby按字母顺序排序数组

时间:2018-04-05 12:54:36

标签: ruby-on-rails arrays ruby sorting alphabetical

我有Array,其中包含混合数据。

就像

["AB_1020", "AB_950", "AB_50", "1000", "570"]

输出应为

AB_50, AB_950, AB_1020, 570, 1000

2 个答案:

答案 0 :(得分:2)

["AB_1020", "AB_950", "AB_50", "1000", "570"].sort_by do |k|
  i, w = k.split('_').rotate
  [w.to_s, -i.to_i]
end.reverse
#⇒ ["AB_50", "AB_950", "AB_1020", "570", "1000"]

答案 1 :(得分:2)

你可以做这样的事情

#⇒ ["AB_50", "AB_950", "AB_1020", "570", "1000"]

ouptut

<button onclick=this.form.submit();>Pay Now</button>