Oops, I'm new to the site, as well as Ruby.
My question is: Is there a more efficient way to sort a hash according to values?
I made 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, & quot; denis & quot; = > 22, "fabio" = > 0}
Expected Outup: agents_ordered = {"denis" = > 22, "gabriel" = > 15, "lucas" = > 12, "pedro" = > 7, "tadeu" = > 4, "fabio" = >
What would be the most efficient way to qualify?