6. Sorting the Hash (Whats wrong with my code?)


#1



https://www.codecademy.com/courses/ruby-beginner-en-693PD/0/6?curriculum_id=5059f8619189a5000201fbcb#


undefined method `reverse!'
Oops, try again.
It looks like you haven't set the frequencies hash correctly.

puts "Write: "
text = gets.chomp
words = text.split
frequencies = Hash.new(0)

words.each do | word |  frequencies[word] +=1 end
puts frequencies

words.each { |word|  puts frequencies[word] }
for word,count in frequencies
   puts "#{word} \t#{count}"
end

frequencies = frequencies.sort_by do |word,count| 
  count
  frequencies.reverse!
end


#2

frequencies = frequencies.sort_by do |word,count| 
  count
  frequencies.reverse!
end

Hint: You need to the reversing after sorting :wink:


#3

That worked, thanks!