How can I sort array of hashes if I have pre-determined order I have an array that needs to be sorted like this :
array = [{:attr_name=>:phone, :attr_value=>30}, {:attr_name=>:name, :attr_value=>20}, {:attr_name=>:similarity, :attr_value=>0}, {:attr_name=>:weight, :attr_value=>50}]
and I've got a hash based on which I want it to be sorted :
pre_sorted = {
:name => 0,
:phone => 1,
:weight=> 2,
:similarity => 3
}
After sorting my array should look like this :
[{:attr_name=>:name, :attr_value=>20}, {:attr_name=>:phone, :attr_value=>30}, {:attr_name=>:weight, :attr_value=>50}, {:attr_name=>"similarity", :attr_value=>0}]
I've looked at the ruby sort and sort by docs and found related questions on So but couldn't figure it out because I'm just starting with rails.
similaritya string in your array, but a symbol in thepre_sortedhash?