【问题标题】:How to group_by year in this type data in Ruby [closed]如何在Ruby中对这种类型的数据进行分组[关闭]
【发布时间】:2014-12-02 07:34:14
【问题描述】:

我想按年份分组:

[{"_id"=>{"year"=>2013, "month"=>8}, "count"=>69605},
 {"_id"=>{"year"=>2013, "month"=>7}, "count"=>60620},
 {"_id"=>{"year"=>2013, "month"=>12}, "count"=>56026},
 {"_id"=>{"year"=>2014, "month"=>5}, "count"=>55067},
 {"_id"=>{"year"=>2014, "month"=>4}, "count"=>51313},
 {"_id"=>{"year"=>2013, "month"=>11}, "count"=>48044},
 {"_id"=>{"year"=>2014, "month"=>8}, "count"=>43802},
 {"_id"=>{"year"=>2014, "month"=>1}, "count"=>40428},
 {"_id"=>{"year"=>2014, "month"=>3}, "count"=>39564},
 {"_id"=>{"year"=>2013, "month"=>9}, "count"=>35682},
 {"_id"=>{"year"=>2013, "month"=>10}, "count"=>34073},
 {"_id"=>{"year"=>2014, "month"=>2}, "count"=>32774},
 {"_id"=>{"year"=>2014, "month"=>6}, "count"=>30772},
 {"_id"=>{"year"=>2014, "month"=>9}, "count"=>17347},
 {"_id"=>{"year"=>2014, "month"=>7}, "count"=>5319},
 {"_id"=>{"year"=>2013, "month"=>6}, "count"=>4468},
 {"_id"=>{"year"=>2013, "month"=>5}, "count"=>2978},
 {"_id"=>{"year"=>2013, "month"=>4}, "count"=>2498},
 {"_id"=>{"year"=>2013, "month"=>3}, "count"=>360},
 {"_id"=>{"year"=>2013, "month"=>1}, "count"=>112},
 {"_id"=>{"year"=>2012, "month"=>12}, "count"=>88},
 {"_id"=>{"year"=>2013, "month"=>2}, "count"=>28},
 {"_id"=>{"year"=>2015, "month"=>11}, "count"=>1}]

谁能帮帮我?

【问题讨论】:

  • 请说明您是如何解决的?
  • 预期结果是什么?

标签: ruby-on-rails ruby mongodb mongoid


【解决方案1】:

试试这个

Your_Array.group_by{|a| a["_id"]["year"]}

【讨论】:

  • 谢谢,这对我有用,非常有帮助:)
【解决方案2】:

您的问题已解决,请检查以下解决方案

Your_array.group_by { |d| d['_id']['year'] } 

【讨论】:

  • 谢谢,这对我有用
【解决方案3】:

Enumerable#reduce 来救援。假设数据存储在 data 变量中:

data.reduce({}) { |memo, item| 
  (memo[item['_id']['year']] ||= []) << item; 
  memo 
}

# => {
#  2012 => [
#    [0] {
#        "_id" => {
#        "month" => 12,
#         "year" => 2012
#      },
#      "count" => 88
#    }
#  ],
#  2013 => [
#    [ 0] {
# ...

可能是您正在寻找的。是否要产生重组数据,只需修改被调用者即可。

【讨论】:

    【解决方案4】:

    我想这对你有用:

    array.group_by{|a| a["_id"]["year"]}.values
    

    【讨论】:

      【解决方案5】:

      使用Enumerable#group_by

      array.group_by { |record| record['_id']['year'] }
      
      #=> {2013=>
      #  [{"_id"=>{"year"=>2013, "month"=>8}, "count"=>69605},
      #   ...
      #   {"_id"=>{"year"=>2013, "month"=>2}, "count"=>28}],
      # 2014=>
      #  [{"_id"=>{"year"=>2014, "month"=>5}, "count"=>55067},
      #   ...
      #   {"_id"=>{"year"=>2014, "month"=>7}, "count"=>5319}],
      # 2012=>[{"_id"=>{"year"=>2012, "month"=>12}, "count"=>88}],
      # 2015=>[{"_id"=>{"year"=>2015, "month"=>11}, "count"=>1}]}
      

      【讨论】:

        【解决方案6】:

        使用Enumerable#group_by

        grouped_array = array.group_by {|record| record["_id"]["year"]}
        

        使用Enumerable#sort_by对记录进行排序

        sorted_array = grouped_array.sort_by{|year, records| year}
        formated_hash = sorted_array.to_h
        

        这将按年份的升序排序。如果要按降序排序,请使用Array.reverse

        desc_sorted_array = sorted_array.reverse
        formated_hash = desc_sorted_array.to_h
        

        【讨论】:

        • 没有必要对哈希进行排序。对键进行排序并对其进行迭代以检索值,或使用values_at 一次检索它们。
        • 说得好,没想到这么说。我想使用 sort_by 会更快。
        • 在这种情况下,sort_by 没有优势。使用sorted_keys = grouped_array.keys.sort 按顺序获取键列表会更快,然后使用该列表提取值,使用grouped_array.values_at(*sorted_keys) 之类的方法按顺序检索它们。
        • sortsort_by 快,如果被排序的对象直接实现 &lt;=&gt; 并且使用的值没有嵌入,如整数和字符串等基本对象。一旦你转向更复杂的对象,你必须使用 getter 或方法来计算用于排序的值,那么sort_by 会快得多。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-03-04
        • 2013-03-30
        • 1970-01-01
        • 2011-10-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多