q = ["2002-h2","2002-h1", "2000-h1", "2005-h2", "2000-h2", "2005-h1"]
q.sort_by { |a| a }.collect{|u| u}
=> ["2000-h1", "2000-h2", "2002-h1", "2002-h2", "2005-h1", "2005-h2"]
当我使用标题化删除连字符symols
时 q.sort_by { |a| a }.collect{|u| u.titleize}
=> ["2000 H1", "2000 H2", "2002 H1", "2002 H2", "2005 H1", "2005 H2"]
我想要这种类型
=> ["2000-H1", "2000-H2", "2002-H1", "2002-H2", "2005-H1", "2005-H2"]
我想要排序q值
如何解决这个问题?
答案 0 :(得分:2)
这只是一个字母吗?然后只是upcase
?
[1] pry(main)> "2000-h1".upcase
=> "2000-H1"
答案 1 :(得分:0)
您可以写得更短,因为.sort_by { |a| a }
返回的内容不仅仅是.sort
而.collect { |a| a.upcase }
返回的内容与.map(&:upcase)
相同
q = ["2002-h2","2002-h1", "2000-h1", "2005-h2", "2000-h2", "2005-h1"]
q.sort.map(&:upcase)
#=> ["2000-H1", "2000-H2", "2002-H1", "2002-H2", "2005-H1", "2005-H2"]