Forum: Ruby Re: Sorting with Hash

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Kroeger, Simon (ext) (Guest)
on 2006-04-27 17:23
(Received via mailing list)
Sorry, after reading your post again I guess I haven't
understand your problem.

Standard Hashes are not sorted, never. If you call
sort on a Hash you get nested arrays.

[["a", 10], ["b", 30], ["c", 20]]

if you want to iterate over this array just use each.

c.each{|k, v| do_something(k, v)}

if you want the value corresponding to a key use assoc.

c.assoc('b') #=> 30

(but this is slow on large arrays)

Hth

Simon
This topic is locked and can not be replied to.