Don't quite understand .sort_by


#1

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

Hi, I'm new to Ruby and really keen on learning. I'm trying to understand how this code words. More specifically, why:

frequencies = frequencies.sort_by { |word, count| , count }

requires the second 'count'?

Below is my code:

puts "Say anything! The more the better!"
text = gets.chomp

words = text.split(" " || ", " || "!" || ".")

frequencies = Hash.new(0)

words.each { |single_words| frequencies[single_words] += 1 }
frequencies = frequencies.sort_by { |word, count| count }
frequencies.reverse!

The code works fine by the way! I'm just trying to understand it properly. Thanks so much. :slight_smile:


#2

This post was flagged by the community and is temporarily hidden.


#3

Oops!

I meant:

frequencies = frequencies.sort_by { |word, count| count }

(without the comma)


#5

Does this mean that if I put 'word' instead of 'count', it gets sorted by the word instead?


#6

This post was flagged by the community and is temporarily hidden.


#7

Oh thank you so much!! I understand now. :slight_smile: