遍历哈希数组,以基于另一个哈希键对一个哈希值的值求和

时间:2018-08-31 09:18:12

标签: ruby

我将以下散列数组作为输入:

ar = [{"Sales"=>"11", "CustID"=>"Cust04"},
      {"Sales"=>"44.9", "CustID"=>"Cust04"},
      {"Sales"=>"79.17", "CustID"=>"Cust06"},
      {"Sales"=>"73.84", "CustID"=>"Cust06"},
      {"Sales"=>"34.9", "CustID"=>"Cust06"},
      {"Sales"=>"29.6825", "CustID"=>"Cust06"},
      {"Sales"=>"2048.7", "CustID"=>"Cust06"},
      {"Sales"=>"15.24", "CustID"=>"Cust02"},
      {"Sales"=>"54.874", "CustID"=>"Cust04"},
      {"Sales"=>"12.79", "CustID"=>"Cust08"},
      {"Sales"=>"22.65", "CustID"=>"Cust08"},
      {"Sales"=>"63.26", "CustID"=>"Cust08"},
      {"Sales"=>"16.207", "CustID"=>"Cust08"},
      {"Sales"=>"782.2", "CustID"=>"Cust07"},
      {"Sales"=>"215.45", "CustID"=>"Cust07"},
      {"Sales"=>"781.23", "CustID"=>"Cust07"},
      {"Sales"=>"370.14", "CustID"=>"Cust07"},
      {"Sales"=>"1.7", "CustID"=>"Cust09"},
      {"Sales"=>"22.405", "CustID"=>"Cust09"}
]

我正在寻找基于销售总额的以下输出,其排名为:

ar_out # =>
["Customer" => "Cust04", "TotalSales" => "xxxx", "Rank" => "1"]

2 个答案:

答案 0 :(得分:2)

您可以尝试这种方式

输入

ar = [
  { "Sales" => "11", "CustID" => "Cust04" },
  { "Sales" => "44.9", "CustID" => "Cust04" },
  { "Sales" => "79.17", "CustID" => "Cust06" },
  { "Sales" => "73.84", "CustID" => "Cust06" },
  { "Sales" => "34.9", "CustID" => "Cust06" },
  { "Sales" => "29.6825", "CustID" => "Cust06" },
  { "Sales" => "2048.7", "CustID" => "Cust06" },
  { "Sales" => "15.24", "CustID" => "Cust02" },
  { "Sales" => "54.874", "CustID" => "Cust04" },
  { "Sales" => "12.79", "CustID" => "Cust08" },
  { "Sales" => "22.65", "CustID" => "Cust08" },
  { "Sales" => "63.26", "CustID" => "Cust08" },
  { "Sales" => "16.207", "CustID" => "Cust08" },
  { "Sales" => "782.2", "CustID" => "Cust07" },
  { "Sales" => "215.45", "CustID" => "Cust07" },
  { "Sales" => "781.23", "CustID" => "Cust07" },
  { "Sales" => "370.14", "CustID" => "Cust07" },
  { "Sales" => "1.7", "CustID" => "Cust09" },
  { "Sales" => "22.405", "CustID" => "Cust09" }
]

过程

ar.each_with_object(Hash.new(0)) { |hsh, e| e[hsh['CustID']] += hsh['Sales'].to_f }.
  sort_by { |_, v| -v }.
  map.with_index { |(k, v), i| [{ 'Customer' => k, 'TotalSales' => v, 'Rank' => i + 1 }] }

输出

[
  [
    {
      "Customer": "Cust06",
      "TotalSales": 2266.2925,
      "Rank": 1
    }
  ],
  [
    {
      "Customer": "Cust07",
      "TotalSales": 2149.02,
      "Rank": 2
    }
  ],
  [
    {
      "Customer": "Cust08",
      "TotalSales": 114.90699999999998,
      "Rank": 3
    }
  ],
  [
    {
      "Customer": "Cust04",
      "TotalSales": 110.774,
      "Rank": 4
    }
  ],
  [
    {
      "Customer": "Cust09",
      "TotalSales": 24.105,
      "Rank": 5
    }
  ],
  [
    {
      "Customer": "Cust02",
      "TotalSales": 15.24,
      "Rank": 6
    }
  ]
]

答案 1 :(得分:1)

UISegmentedControl