How to sort a hash by value in descending order and output a hash in ruby? July 28, 2023 by Tarik Try: Hash[h.sort.reverse] This should return what you want. Edit: To do it by value: Hash[h.sort_by{|k, v| v}.reverse]