Ruby - 使用动态键排序

时间:2014-07-21 17:22:08

标签: ruby arrays hash

我有一系列哈希:

array = [
    {
        id: 1,
        name: "A",
        points: 20,
        victories: 4,
        goals: 5,
    },
    {
        id: 1,
        name: "B",
        points: 20,
        victories: 4,
        goals: 8,
    },
    {
        id: 1,
        name: "C",
        points: 21,
        victories: 5,
        goals: 8,
    }
]

使用两个键对它们进行排序:

array = array.group_by do |key| 
[key[:points], key[:goals]] 
end.sort_by(&:first).map(&:last)

但在我的程序中,sort标准存储在数据库中,我可以将它们存储在一个数组中,例如:["goals","victories"]["name","goals"]

如何使用dinamic键对数组进行排序?

我尝试了许多方法但没有成功:

criterias_block = []
criterias.each do |criteria|
    criterias_block << "key[:#{criteria}]"
end

array = array.group_by do |key| 
    criterias_block 
end.sort_by(&:first).map(&:last)

3 个答案:

答案 0 :(得分:5)

Array#sort可以做到这一点

criteria = [:points, :goals]
array.sort_by { |entry|
  criteria.map { |c| entry[c] }
}

#=> [{:id=>1, :name=>"A", :points=>20, :victories=>4, :goals=>5},
#    {:id=>1, :name=>"B", :points=>20, :victories=>4, :goals=>8},
#    {:id=>1, :name=>"C", :points=>21, :victories=>5, :goals=>8}]

这是有效的,因为如果你对数组[[1,2], [1,1], [2,3]]进行排序,它会按照第一个元素进行排序,使用任何下一个元素来断开关系

答案 1 :(得分:1)

您可以使用values_at

criteria = ["goals", "victories"]
criteria = criteria.map(&:to_sym)
array = array.group_by do |key| 
    key.values_at(*criteria)
end.sort_by(&:first).map(&:last)

# => [[{:id=>1, :name=>"A", :points=>20, :victories=>4, :goals=>5}, 
#      {:id=>1, :name=>"B", :points=>20, :victories=>4, :goals=>8}, 
#      {:id=>1, :name=>"C", :points=>21, :victories=>5, :goals=>8}]]

values_at返回所请求的所有键的数组:

array[0].values_at(*criteria)
# => [4, 5]

答案 2 :(得分:1)

我建议这样做。

<强>代码

def sort_it(array,*keys)
  array.map { |h| [h.values_at(*keys), h] }.sort_by(&:first).map(&:last)
end  

<强>实施例

由您提供的array

sort_it(array, :goals, :victories)
  #=> [{:id=>1, :name=>"A", :points=>20, :victories=>4, :goals=>5},
  #    {:id=>1, :name=>"B", :points=>20, :victories=>4, :goals=>8}, 
  #    {:id=>1, :name=>"C", :points=>21, :victories=>5, :goals=>8}]
sort_it(array, :name, :goals)
  #=> [{:id=>1, :name=>"A", :points=>20, :victories=>4, :goals=>5},
  #    {:id=>1, :name=>"B", :points=>20, :victories=>4, :goals=>8},
  #    {:id=>1, :name=>"C", :points=>21, :victories=>5, :goals=>8}]

对于第一个例子,你当然可以写:

sort_it(array, *["goals", "victories"].map(&:to_sym))