A running total is a pretty classic case for reduce
. All you need to do is add another reduce
after the sorting to compute the running total and stash it in the objects, something like:
running_total = (t, p) -> p.Total = t + p.Product
_(sorted).reduce running_total, 0
That will give you a sorted like this:
sorted = [
{ Price: 1, Money: 2, Product: 2, Total: 2 }
{ Price: 2, Money: 10, Product: 5, Total: 7 }
{ Price: 3, Money: 9, Product: 3, Total: 10 }
]