I need to sort an Hash according to order of keys present in other array:
hash = { a: 23, b: 12 }
array = [:b, :a]
required_hash #=> { b: 12, a: 23 }
Is there any way to do it in single line?
hash = { a: 23, b: 12 }
array = [:b, :a]
array.zip(hash.values_at(*array)).to_h
#=> {:b=>12, :a=>23}
The steps:
v = hash.values_at(*array)
#=> [12, 23]
a = array.zip(v)
#=> [[:b, 12], [[:a, 23]]
a.to_h
#=> {:b=>12, :a=>23}
You can try something like this:
array = [:b, :a]
{ a: 23, b: 12 }.sort_by { |k, _| array.index(k) }.to_h
#=> {:b=>12, :a=>23}
If
hash = { a:23, b:12 }
array = [:b, :a]
As a one liner:
Hash[array.map{|e| [e, hash[e]]}]
As for ordering, it was some time ago, when ruby changed the implementation where the hash keys are ordered according to insert order. (Before that there was no guarantee that keys would be sorted one way or another).
I do not see any sorting, I think essentially you are doing this:
hash = { a:23, b:12 }
array = [:b, :a]
res = {}
array.each { |key| res[key] = hash[key] } && res # `&& res` is just syntax sugar to return result in single line
#=> {:b=>12, :a=>23}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With