Skip to Content
Create a Histogram
Sorting the Hash

Great! We have a hash full of word / frequency key-value pairs. Now we need to figure out a way to get our information in the order we want it.

colors = { "blue" => 3, "green" => 1, "red" => 2 } colors = colors.sort_by do |color, count| count end colors.reverse!
  1. In the example above, we first create a hash called colors that maps color strings to numbers.
  2. Then, we sort colors into green, red, and blue, from smallest to largest by count. Just so you know, the .sort_by function returns an array of arrays, but that’s fine for our purposes.
  3. Finally, we reverse the array order so that the colors with the largest counts are first.

Can you do the same thing with the frequencies hash?



Use .sort_by to sort the frequencies hash by word count, like step 2 above. Store the result back in frequencies.

Use .reverse! to reverse the sorted frequencies array.

Check the Hint for more details!

Folder Icon

Take this course for free

Already have an account?