1
Oops, I’m new to the site, and I’m new to Ruby.
My question is: Is there a more efficient way to classify a hash according to the values?
I did the code below, but I’m sure it’s not the most efficient way.
agents = {"pedro" => 7, "lucas" => 12, "gabriel" => 15, "tadeu" => 4, "denis" => 22, "fabio" => 0}
agents_array = agents.sort_by { |k, v| v }.reverse
agents_array.map { |element| element.rotate! 1 }
agents_ordered = Hash.new
agents_array.each do |element|
agents_ordered[element[1]] = agents[element[1]]
end
puts agents_ordered.inspect
Input: Agents = {"pedro" => 7, "Lucas" => 12, "Gabriel" => 15, "Tadeu" => 4, "Denis" => 22, "Fabio" => 0}
Expected outup: agents_ordered = {"Denis"=>22, "Gabriel"=>15, "Lucas"=>12, "pedro"=>7, "Tadeu"=>4, "Fabio"=>0}
What would be the most efficient way to classify?