Sorting a hash by value then by key (but the key is reversed)

后端 未结 3 538
梦毁少年i
梦毁少年i 2021-01-15 08:28

The title says it all.

total = {\"Bob\"=>37, \"Alice\"=>42, \"Carl\"=>42}

I want to sort it by value, and then by key, but with th

3条回答
  •  天涯浪人
    2021-01-15 08:43

    Another way:

    total.sort_by { |k,v| [-v,k] }.reverse.to_h
      #=> {"Bob"=>37, "Carl"=>42, "Alice"=>42}
    

提交回复
热议问题