如何通过多个字符串降序对Ruby 2D数组进行排序?

时间:2015-03-01 22:33:29

标签: ruby-on-rails ruby arrays sorting

我在Ruby(1.9.3)中有一个数组,其中每个元素描述了一个机场的几个参数:

@airport_array = Array.new    
@airports.each do |airport|
  @airport_array.push({:id => airport.id, :iata_code => airport.iata_code, :city => airport.city, :country => airport.country})
end

特别是:city:country都是字符串,我希望能够按字母顺序按字母顺序排序,然后按字母顺序排列。

我能够使用类似的东西对整数进行排序:

@airport_array = @airport_array.sort_by {|airport| [-airport[:country], airport[:city]]}

但是,这种语法(特别是使用 - 符号表示反向排序)似乎不适用于字符串。我收到以下错误:

undefined method `-@' for "United States":String

如果我删除减号,我不会收到错误,但正如预期的那样,两个参数的排序都是按字母顺序排列的。

有没有办法可以用两个字符串对这个数组进行排序,只有一个字符串按反向字母顺序排列?

举个例子,假设我有以下数组:

[{:id=>1, :iata_code=>"SEA", :city=>"Seattle", :country=>"United States"},
{:id=>2, :iata_code=>"DEN", :city=>"Denver", :country=>"United States"},
{:id=>3, :iata_code=>"YVR", :city=>"Vancouver", :country=>"Canada"},
{:id=>4, :iata_code=>"HNL", :city=>"Honolulu", :country=>"United States"},
{:id=>5, :iata_code=>"YOW", :city=>"Ottawa", :country=>"Canada"},
{:id=>6, :iata_code=>"YHZ", :city=>"Halifax", :country=>"Canada"}]

在我对它进行排序之后,我希望得到以下数组:

[{:id=>2, :iata_code=>"DEN", :city=>"Denver", :country=>"United States"},
{:id=>4, :iata_code=>"HNL", :city=>"Honolulu", :country=>"United States"},
{:id=>1, :iata_code=>"SEA", :city=>"Seattle", :country=>"United States"},
{:id=>6, :iata_code=>"YHZ", :city=>"Halifax", :country=>"Canada"},
{:id=>5, :iata_code=>"YOW", :city=>"Ottawa", :country=>"Canada"},
{:id=>3, :iata_code=>"YVR", :city=>"Vancouver", :country=>"Canada"}]

所以这些国家是反向字母顺序(U,C),然后在一个国家内,城市按字母顺序排列(D,H,S和H,O,V)。

3 个答案:

答案 0 :(得分:3)

我以为我会使用Enumerable#sort_by来解决这个问题,但我遇到了你所做的同样的问题,所以我使用了一个块。已知带有块的Enumerable#sort比sort_by慢,所以我很好奇别人怎么回答这个问题。

我使用了它:

arr.sort { |a, b| [b[:country], a[:city]] <=> [a[:country], b[:city]] }

看起来像这样:

[76] pry(main)> arr
=> [{:id=>1, :iata_code=>"SEA", :city=>"Seattle", :country=>"United States"},
 {:id=>2, :iata_code=>"DEN", :city=>"Denver", :country=>"United States"},
 {:id=>3, :iata_code=>"YVR", :city=>"Vancouver", :country=>"Canada"},
 {:id=>4, :iata_code=>"HNL", :city=>"Honolulu", :country=>"United States"},
 {:id=>5, :iata_code=>"YOW", :city=>"Ottawa", :country=>"Canada"},
 {:id=>6, :iata_code=>"YHZ", :city=>"Halifax", :country=>"Canada"},
 {:id=>2, :iata_code=>"DOV", :city=>"Dover", :country=>"United States"}]

[77] pry(main)> arr.sort { |a, b| [b[:country], a[:city]] <=> [a[:country], b[:city]]  }
=> [{:id=>2, :iata_code=>"DEN", :city=>"Denver", :country=>"United States"},
 {:id=>2, :iata_code=>"DOV", :city=>"Dover", :country=>"United States"},
 {:id=>4, :iata_code=>"HNL", :city=>"Honolulu", :country=>"United States"},
 {:id=>1, :iata_code=>"SEA", :city=>"Seattle", :country=>"United States"},
 {:id=>6, :iata_code=>"YHZ", :city=>"Halifax", :country=>"Canada"},
 {:id=>5, :iata_code=>"YOW", :city=>"Ottawa", :country=>"Canada"},
 {:id=>3, :iata_code=>"YVR", :city=>"Vancouver", :country=>"Canada"}]

答案 1 :(得分:1)

这个答案应该被视为一种好奇心。它假定所有国家/地区名称都是一个单词。

arr = [
  {:id=>1, :iata_code=>"SEA", :city=>"Seattle",   :country=>"United States"},
  {:id=>2, :iata_code=>"DEN", :city=>"Denver",    :country=>"United States"},
  {:id=>3, :iata_code=>"YVR", :city=>"Vancouver", :country=>"Canada"},
  {:id=>4, :iata_code=>"HNL", :city=>"Honolulu",  :country=>"United States"},
  {:id=>5, :iata_code=>"YOW", :city=>"Ottawa",    :country=>"Canada"},
  {:id=>6, :iata_code=>"YHZ", :city=>"Halifax",   :country=>"Canada"}]

arr.sort_by { |h| [-h[:country].downcase.to_i(36), h[:city]] }
  #=> [{:id=>2, :iata_code=>"DEN", :city=>"Denver",    :country=>"United States"},
  #    {:id=>4, :iata_code=>"HNL", :city=>"Honolulu",  :country=>"United States"},
  #    {:id=>1, :iata_code=>"SEA", :city=>"Seattle",   :country=>"United States"},
  #    {:id=>6, :iata_code=>"YHZ", :city=>"Halifax",   :country=>"Canada"},
  #    {:id=>5, :iata_code=>"YOW", :city=>"Ottawa",    :country=>"Canada"},
  #    {:id=>3, :iata_code=>"YVR", :city=>"Vancouver", :country=>"Canada"}] 

答案 2 :(得分:0)

你可以使用你所在国家/地区第一个字母的ASCII值 - 使用String#ord输出一个整数 - 用于反向排序要求,如下所示:

@airport_array.sort_by {|airport| [-airport[:country][0].ord, airport[:city]]}

编辑:此选项不保证以相同的字母开头的正确种类的不同国家,例如“柬埔寨”和“加拿大”,所以让我们在此技术的基础上考虑所有字母名字。

我想出了这个函数来获取给定字符串中的转置字,将转置字定义为ascii表中转置字母的总和(即'A'变为'Z','C'变为'X'等等,有效地获得一个单词,你可以按逆向字母顺序使用。

# For each upcased letter, transpose the letter according to the ascii table 
# considering that 'A'.ord => 65 and 'Z'.ord => 90, 
# using 65 - X + 90 to obtain the ascii value for the transposed letter.
#
# Usage examples:
#   ascii_inverse('A') => 'Z'
#   ascii_inverse('Denver') => "WVMEVI"
#   ascii_inverse('DENVER') => "WVMEVI"

def ascii_inverse(text)
  text.upcase.chars.map{ |char| (155 - char.ord).abs }.map(&:chr).join
end

现在,您可以将此方法用于sort_by语句:

@airport_array.sort_by {|airport| [ascii_inverse(airport[:country]), airport[:city]]}

最后,我要说我认为这只是一个练习,看看我走多远这条路。虽然它有效但我不愿意使用这种方法,除非有明显的性能优势而且我需要它,而且我可能会更简单的@ Anthony的sort方法。