6. Sorting the Hash



So, here is my code

puts "Hello, write down anything"
text = gets.chomp

words = text.split

frequencies = Hash.new 0

words.each do |key|
    frequencies[key] += 1

frequencies = frequencies.sort_by do |freqencies, count|


For the system everything is okay, but I think something is wrong. Maybe everything is right, but I don't get it, so if you can, please explain to me.

If my input is something like: dog, cat, lion, tiger, wolf. It returns this [["wolf", 1], ["tiger,", 1], ["lion,", 1], ["cat,", 1], ["dog,", 1]]. It only reverses ordering of words, shouldn't it also increment numbers beside? I mean: [["wolf", 1], ["tiger", 2], ["lion", 3]]... and so on?


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.