Here’s what I’m doing atm:
test = {
'd' => 20,
'b' => 40,
'c' => 30,
'a' => 50,
'e' => 10
}
f = []
test.to_a.sort.each do |e|
f << e[1]
end
puts f.join(' ')
Outputs:
50 40 30 20 10
Is there a more efficient/concise/better way to do this? And before someone says so, no, I can’t use an array. :p
EDIT Sorry, posted the wrong code.
In your title you mention that you want to get the values of the array sorted by their respective keys. But in your example, you actually just sort the values, regardless of the keys.
If that’s what you want, simply use:
But if you want to sort the values based on the keys, use this: