fch*_*sen 1 ruby ruby-on-rails
我有一个标题列表,我从数据库中按字母顺序排序,即:
[
'Morning Glory',
'Red',
'Skyline',
'The Next Three Days'
]
Run Code Online (Sandbox Code Playgroud)
什么是重新排序这个标题列表忽略"The"的最佳方式,以便它将成为:
[
'Morning Glory',
'The Next Three Days',
'Red',
'Skyline'
]
Run Code Online (Sandbox Code Playgroud)
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"]
Run Code Online (Sandbox Code Playgroud)