我有一个从数据库中提取的标题列表,按字母顺序排序,即:
[
'Morning Glory',
'Red',
'Skyline',
'The Next Three Days'
]如果忽略" the“,重新排序标题列表的最佳方式是什么?这样它就会变成:
[
'Morning Glory',
'The Next Three Days',
'Red',
'Skyline'
]发布于 2010-11-23 09:08:18
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"]发布于 2010-11-23 09:01:23
你能做的就是使用一个自定义的比较器进行排序。
请参阅:http://rosettacode.org/wiki/Sort_using_a_custom_comparator#Ruby
https://stackoverflow.com/questions/4251742
复制相似问题