I have an array of hashes like this:
items = [{"id"=>"123", "code"=>"abc","name"=>"test", "type"=>["good"]},
{"id"=>"555", "code"=>"ddd","name"=>"foo", "type"=>["better"]},
{"id"=>"098", "code"=>"zyx","name"=>"bar", "type"=>["best"]}]
I am trying to sort each hash within the array by the key.
I tried this:
items.each { |item| item = item.sort.to_h }
It returns the same result:
[{"id"=>"123", "code"=>"abc", "name"=>"test", "type"=>["good"]},
{"id"=>"555", "code"=>"ddd", "name"=>"foo", "type"=>["better"]},
{"id"=>"098", "code"=>"zyx", "name"=>"bar", "type"=>["best"]}]
but when I try this:
items[0].sort.to_h
this is the result:
{"code"=>"abc", "id"=>"123", "name"=>"test", "type"=>["good"]}
So it looks like when I call the individual elements within items
using items[x]
where x
is an index value within the array, it sort it.
But I need a solution to iterate through each element doing that and saving the sort.
Any thoughts?