I'm currently sorting an array of arrays (of numbers) in Ruby with this code:
grapes_sorted = vintages_grapes.group_by(&:itself).sort_by do |k, v| -v.size end.map(&:first)
It works very well. However, I want to store the counters corresponding to each sort in another array.
I tried:
grapes_sorted_counters = []
grapes_sorted = vintages_grapes.group_by(&:itself).sort_by do |k, v|
-v.size
grapes_sorted_counters << v.size
end.map(&:first)
It store the counters, however, the sort is broken, not ordered like it should be. I suppose v.size (and not -v.size) is the cause of the problem in the block.
How can I store the number of occurrences properly in grapes_sorted_counters[] ? Thanks.