我有許多包含相同鍵的哈希,但是彼此的順序都不一致。我需要重新排序每一個以使它們適合正確的順序:使哈希鍵與哈希常數的順序匹配
correct_order = {
:cat => "cat",
:dog => "dog",
:bear => "bear",
:zebra => "zebra",
:monkey => "monkey"
}
hash1 = {
:bear => "bear",
:cat => "cat"
}
hash2 = {
:cat => "cat",
:monkey => "monkey",
:zebra => "zebra",
:bear => "bear"
}
hash3 = {
:dog => "dog",
:monkey => "monkey",
:cat => "cat"
}
我怎麼會去比較HASH1,HASH2和hash3的關鍵爲了使每個鍵匹配其在correct_order散的地方嗎? hash2將變爲:
hash2 {
:cat => "cat",
:bear => "bear",
:zebra => "zebra",
:monkey => "monkey"
}
請注意,每個新的散列不一定具有所有的鍵。每個哈希將具有變化的大小和變化的順序。
謝謝