【发布时间】:2019-08-30 04:12:20
【问题描述】:
我有一个当前按字母顺序排序的数组,我正在尝试按字符串的手动顺序对其进行排序。
当前代码:
list = ["gold","silver","bronze","steel","copper"]
list = list.sort { |a, b| a <=> b }
我想要实现的目标:(以空白条目作为分隔符)
list = ["gold","silver","bronze","steel","copper"]
sort_order = ["bronze","silver","gold","","copper","steel"]
list = list.sort_by sort_order
输出:青铜 |银 |黄金 | - |铜| 家企业钢
这可能吗?目前遇到这些错误消息:
comparison of Integer with nil failed
comparison of String with String failed
【问题讨论】:
标签: ruby-on-rails arrays ruby string sorting