Ruby on Rails Friday, May 29, 2015

On May 29, 2015, at 7:24 AM, Gm <javaplayer@gmail.com> 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 output do you want? Possibly an array structured (I'm not showing it sorted) something like: [[4049,[4133,100]],[5814,[4075,84]]…]. Rails provides a kind of hybrid thing, OrderedHash, which you could build—but you'd have to build an array and then sort it anyway in order to be able to insert items into an OrderedHash in sort order.

--
Scott Ribe
scott_ribe@elevated-dev.com
http://www.elevated-dev.com/
https://www.linkedin.com/in/scottribe/
(303) 722-0567 voice





--
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/B26AE3AA-CD6B-48B4-9EF9-DCA4ABD23503%40elevated-dev.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment