Hello I\'m trying to find the largest value in my hash. I made a search in google and I found this code:
def largest_hash_key(hash)
key = hash.sort{|a,b|
Sort the hash once instead of finding max. This way you can also get smallest etc.
def reverse_sort_hash_value(hash)
hash = hash.sort_by {|k,v| v}.reverse
end
h = reverse_sort_hash_value(h)
Key of largest value
max = *h[0][0]
Get Key/Value of the smallest value
puts *h[h.length-1]
You can convert to hash using Hash[h.select { |k, v| v == max}] or using h.to_h