When I use sort_by on the frequency hash, it returns an array. How do I return a hash instead?
puts frequency.class #returns hash
frequency = frequency.sort_by {|k,v| v}.reverse
puts frequency.class #returns array
sort_by just returns an array. You can cast it back to a hash like this:
frequency = frequency.sort_by {|k,v| v}.reverse
frequency = Hash[frequency]
each) changes, but it's not sorted.ruby 1.9.3. I tried this code for instance: h = {:fernando => 28,:john => 18,:peter => 54} Hash[h.sort_by { |name, age| age }.reverse].reverse at the end. It would give you the values order by age starting by the oldest onto the youngest.