在Rails中对部分排序的列表进行排序

这个问题最初源于我在这里发表的另一篇SOpost。 然而,问题实际上是一个Rails问题,我试图解决问题,使其更简单,这将使我的答案更易于管理,而不是让有兴趣帮助的人感到沮丧。 这是最初的问题:

在我的初始化文件夹中,我有一个非常复杂的集合,一个哈希与数组的哈希。 该集合如下所示:

Locations = { :Australia => { :cities => %w(Sidney Melbourne Other), :currency => 'AUD'}, :Canada => { :cities => %w(Montreal Toronto Other), :currency => 'CAD'}, :England => { :cities => %w(London Other), :currency => 'GBP'}, :Israel => { :cities => ['Jerusalem', 'Tel Aviv', "Modi'in", 'Netanya', 'Haifa', 'Other'], :currency => 'ILS'}, :USA => { :cities => ['Chicago', 'Los Angeles', 'Miami', 'Teaneck', 'New York', 'Brooklyn', 'Queens', 'Bronx', 'Washington', 'Other'], :currency => 'USD'}, :France => { :cities => %w(Paris Other), :currency => 'EUR'}, :Switzerland => { :cities => %w(Zurich Antwerp Other), :currency => 'CHF'}, :South_Africa => { :cities => ['Johannesburg', 'Cape Town', 'Other'], :currency => 'ZAR'}, :Argentina => { :cities => ['Buenos Aires', 'Other'], :currency => 'ARS'} } 

在我的观点中,我使用初始化程序对所选国家/地区的城市进行排序,具体如下:

  tr -city_select = Locations.collect { |country_name, country_info | country_info[:cities].collect {|city| [city, city, class: country_name] } }.flatten(1) th = f.label :city, '*City' td = f.select :city, city_select, {}, id: 'property_city' 

问题是,当我选择下拉列表时,城市,在选择国家/地区后,列表将被排除。 如果我刚刚在块{| city |上添加了.sort [city,city,class:country_name]}。排序,城市组将按字母顺序排序,包括“其他”一词。 我不知道如何对城市列表进行部分排序,并在最后附加成员“其他”。

感谢@wayneThis回答我的问题:

  tr -city_select = Locations.collect { |country_name, country_info | country_info[:cities].collect {|city| [city, city, class: country_name] }.sort_by { |(city)| [city == "Other" ? 1 : 0, city]}.push(["Other"]) }.flatten(1) th = f.label :city, '*City' td = f.select :city, city_select, {}, id: 'property_city' 

感谢@WayneConrad和@bjhaid,我能够解决我的问题。