6. Sorting the Hash - Understanding the sort_by for a default value


6. Sorting the Hash

I'm trying my best to understand the theory behind the sort_by method. After reviewing previous hashes, I understanding sorting clear key/value pair hashes like this...

my_random_hash = {
  "blue" => 1,
  "red" => 2

my_random_hash.sort_by do |color, value|

This makes sense to me seeing as though we've created a key/value pair on our own, in which we can sort "clearly".

However, in this particular exercise we're sorting a "default value" of 0, or one which we're asking our "user" to create by generating the line

frequencies = Hash.new (0).

Which is a hash that does not contain key/value pairs.

My question is, this tasks code requires us to input this code:

frequencies = frequencies.sort_by do |x, y|

... does sorting a default value ALWAYS require us to input two variables (in my example x and y) and do they ALWAYS signify sorting by WORD and COUNT?


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