How to group and sum arrays in Ruby?
arrays, ruby
Solution
`ar.group_by(&:last).map{ |x, y| [y.inject(0){ |sum, i| sum + i.first }, x] }`
Edit to add explanation: We group by the last value (the date) yielding a hash:
{"2014-01-27"=>[[5, "2014-01-27"]], "2014-01-28"=>[[20, "2014-01-28"], [5, "2014-01-28"], [10, "2014-01-28"]], "2014-01-29"=>[[15, "2014-01-29"], [5, "2014-01-29"]], "2014-01-30"=>[[5, "2014-01-30"], [10, "2014-01-30"], [5, "2014-01-30"]]}
Then map that with `x` as they hash key, and `y` as the array of `[[number, date], [number, date]]` pairs.
`.inject(0)` means `sum` starts out as `0`, then we add the first item of each array (the number) to that sum until all arrays are iterated and all the numbers are added.
Then we do `[y, x]` where `x` is the hash key (the date), and `y` is the sum of all the numbers.
This method is efficient as we use inject to avoid mapping the array twice and don't have to reverse the values afterwards since we swapped their positions while mapping it.
Edit: Interestingly the benchmarks between @bjhaid and my answer are close:
user system total real
5.117000 0.000000 5.117000 ( 5.110292)
5.632000 0.000000 5.632000 ( 5.644323)
`1000000` iterations - my method was the slowest
Problem
I have an array of arrays like this: ``` ar = [[5, "2014-01-27"], [20, "2014-01-28"], [5, "2014-01-28"], [10, "2014-01-28"], [15, "2014-01-29"], [5, "2014-01-29"], [5, "2014-01-30"], [10, "2014-01-30"], [5, "2014-01-30"]] ``` What I ultimately need to do is group the array items by date and sum up the numbers in the first item of each sub-array. So output would be something like: ``` [[5, "2014-01-27"], [35, "2014-01-28"], [20, "2014-01-29"], [20, "2014-01-30"]] ```