如何更改哈希的顺序?

时间:2019-04-16 13:27:54

标签: ruby hash

我有以下哈希值:

{"match"=>0, "reach"=>1, "safe"=>2, "undecided"=>3}

我想更改如下顺序:

{"reach"=>1, "match"=>0, "safe"=>2, "undecided"=>3}

最有效的解决方案是什么?

1 个答案:

答案 0 :(得分:5)

{"match"=>0, "reach"=>1, "safe"=>2, "undecided"=>3}
.slice("reach", "match", "safe", "undecided")
# => {"reach"=>1, "match"=>0, "safe"=>2, "undecided"=>3}