2014-02-20 69 views
0

我使用下面的代碼分組數組項目,但它感覺很慢。 有更快/更好的分組方法嗎?如何以最快的方式對數組進行分組?

@tasks_sorted = @tasks.group_by(&:start_date).map do |month, data| 

     hash = {"date" => month} 

     data.each {|placement| hash["tasks"] = data} 

     hash 

    end 
+1

的代碼誤導邏輯..不明確 –

回答

3

我想下面你正在尋找:

@tasks_sorted = @tasks.group_by(&:start_date).map do |month, data| 
    {"date" => month, "tasks" => data } 
end 
+2

的OP的意圖是不明確的,但是這個代碼是至少相當於OP的。我希望它不會再被忽略。 – sawa

+0

再次感謝你們,我再也不會忽視任何人;) –

相關問題