我有一个按字母顺序从数据库中提取的标题列表,即:
[
'Morning Glory',
'Red',
'Skyline',
'The Next Three Days'
]
对这个标题列表进行重新排序的最佳方法是忽略“The”,以便它成为:
[
'Morning Glory',
'The Next Three Days',
'Red',
'Skyline'
]
答案 0 :(得分:8)
titles = ["Morning Glory", "Red", "Skyline", "The Next Three Days"]
titles.sort_by {|w| w.sub(/^the /i,"")}
=> ["Morning Glory", "The Next Three Days", "Red", "Skyline"]
答案 1 :(得分:1)
您可以做的是使用自定义比较器进行排序。
请参阅:http://rosettacode.org/wiki/Sort_using_a_custom_comparator#Ruby