Best way to get the values of a hash ordered by their respective keys? (Ruby)

Posted by they changed my name on Stack Overflow See other posts from Stack Overflow or by they changed my name
Published on 2010-04-24T16:59:48Z Indexed on 2010/04/24 17:03 UTC
Read the original article Hit count: 125

Filed under:
|
|

Here's what I'm doing atm:

test = {
  'd' => 20,
  'b' => 40,
  'c' => 30,
  'a' => 10,
  'e' => 50
}

f = []
test.to_a.each do |e|
  f << e[1]
end
puts f.sort.join(' ')

Outputs:

10 20 30 40 50

Is there a more efficient/concise/better way to do this? And before someone says so, no, I can't use an array. :p

© Stack Overflow or respective owner

Related posts about ruby

Related posts about hash