Ruby on Rails Monday, June 1, 2015



On Mon, Jun 1, 2015 at 7:03 AM, Gm <javaplayer@gmail.com> 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 to an array (.to_a) then reversing and then turning back into a hash (.to_h).

Assuming you don't need the intermediate results, you can skip a couple of those by just doing,

Hash[hash.sort_by { |_, v| v[1] }.reverse]

or,

hash.sort_by { |_, v| v[1] }.reverse.to_h  # if you prefer .to_h

There's an interesting stackoverflow analysis on ways of descending order sorts in Ruby. Summary: sort_by + reverse is the fastest, slightly ahead of the -v hack (fewer ops presumably). http://stackoverflow.com/a/2651028/683947

Paul



Thanks.

On Friday, May 29, 2015 at 6:14:53 PM UTC-3, Paul Makepeace wrote:
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.

sorted_by_second_value
​.map { |_, v| v[1] }
 => [74, 75, 84, 99, 100]

​Paul​


On Fri, May 29, 2015 at 6:24 AM, Gm <javap...@gmail.com> 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 this problem ? I'm using sort method but can't work it out.

Thanks.


--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-ta...@googlegroups.com.
To post to this group, send email to rubyonra...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/3ea488c8-cd97-4f03-b31e-f27b44c009ee%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/1018b076-41d5-456c-b89f-374a10079276%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/CAMEJyivQGdS%2BPPguiv3DSy_jZib85yO_cAFD4Es%2BzSpyDCoFuA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment