Re: [Rails] Sort hash in model

2015-06-01 Thread Paul Makepeace
On Mon, Jun 1, 2015 at 7:03 AM, Gm wrote: > Thank you all. > > Paul, I end up doing this: > > sorted_by_second_value = Hash[hash.sort_by { |_, v| v[1] }] > sorted_by_second_value.to_a.reverse.to_h > ​Here you're converting an array (​hash.sort_by { |_, v| v[1] }) to a hash ( Hash[...]) then back

Re: [Rails] Sort hash in model

2015-06-01 Thread Gm
Thank you all. Paul, I end up doing this: sorted_by_second_value = Hash[hash.sort_by { |_, v| v[1] }] sorted_by_second_value.to_a.reverse.to_h Thanks. On Friday, May 29, 2015 at 6:14:53 PM UTC-3, Paul Makepeace wrote: > > sorted_by_second_value = Hash[hash.sort_by >

Re: [Rails] Sort hash in model

2015-05-29 Thread Paul Makepeace
sorted_by_second_value = Hash[hash.sort_by { |_, v| v[1] }] For those saying ruby hashes are unordered: http://ruby-doc.org/core-2.2.2/Hash.html Hashes enumerate their values in the order that the corresponding keys were inserted.

Re: [Rails] Sort hash in model

2015-05-29 Thread Rob Biedenharn
On 2015-May-29, at 09:24 , Gm wrote: > Hi, I have this hash: > > hash = { 4049=>[4133, 100], 5814=>[4075, 84], 382543=>[4064, 74], > 382544=>[4065, 99], 382545=>[4066, 75] } > > I need to sort (DESC) this hash by the second item of array. > Example: 100, 84, 74, 99, 75 > > How can I solve th

Re: [Rails] Sort hash in model

2015-05-29 Thread Scott Ribe
On May 29, 2015, at 7:24 AM, Gm wrote: > > I need to sort (DESC) this hash by the second item of array. > Example: 100, 84, 74, 99, 75 > > How can I solve this problem ? I'm using sort method but can't work it out. You can’t really sort a regular hash—by definition a hash is unordered. So what

[Rails] Sort hash in model

2015-05-29 Thread Gm
Hi, I have this hash: hash = { 4049=>[4133, 100], 5814=>[4075, 84], 382543=>[4064, 74], 382544=>[4065, 99], 382545=>[4066, 75] } I need to sort (DESC) this hash by the second item of array. Example: 100, 84, 74, 99, 75 How can I solve this problem ? I'm using sort method but can't work it out.