ruby - Sort Nested Data Structure -


given nested hash (see below) sort siblings have ':position' attribute.

in hash below can see child , child_child attributes being array position isn't ordered.

{:parent=>       [{:id=>"29637484-4d39-4828-bebc-52e4ecb12250",         :extra_data=>"parent_extra_data_1two",         :position=>2,         :resource_id=>"parent_resource_id_1two",         :child=>          [{:id=>"57c9dab3-f091-48e7-85a7-4a8a3c4782a6", :extra_data=>"child_extra_data_1two", :position=>2, :resource_id=>"child_resource_id_2two"},           {:id=>"86b60506-f431-41f4-8555-30a83e9296f9",            :extra_data=>"child_extra_data_1",            :position=>1,            :resource_id=>"child_resource_id_1",            :child_child=>             [{:id=>"04bb9af2-eac8-4fcf-8253-38b0ccb538c2", :extra_data=>"child_child_extra_data_one", :position=>1, :resource_id=>"child_child_one"},              {:id=>"94947cc4-e27a-4f79-b585-f26db7ce4e66", :extra_data=>"child_child_extra_data_three", :position=>3, :resource_id=>"child_child_three"},              {:id=>"98fe0b96-a8cd-488b-bf00-9d48633355d3", :extra_data=>"child_child_extra_data_two", :position=>2, :resource_id=>"child_child_two"}]}]}]} 

would become

{:parent=>       [{:id=>"29637484-4d39-4828-bebc-52e4ecb12250",         :extra_data=>"parent_extra_data_1two",         :position=>2,         :resource_id=>"parent_resource_id_1two",         :child=>          [{:id=>"86b60506-f431-41f4-8555-30a83e9296f9",            :extra_data=>"child_extra_data_1",            :position=>1,            :resource_id=>"child_resource_id_1",            :child_child=>               [{:id=>"04bb9af2-eac8-4fcf-8253-38b0ccb538c2", :extra_data=>"child_child_extra_data_one", :position=>1, :resource_id=>"child_child_one"},               {:id=>"98fe0b96-a8cd-488b-bf00-9d48633355d3", :extra_data=>"child_child_extra_data_two", :position=>2, :resource_id=>"child_child_two"},               {:id=>"94947cc4-e27a-4f79-b585-f26db7ce4e66", :extra_data=>"child_child_extra_data_three", :position=>3, :resource_id=>"child_child_three"}]},           {:id=>"57c9dab3-f091-48e7-85a7-4a8a3c4782a6",            :extra_data=>"child_extra_data_1two",            :position=>2,            :resource_id=>"child_resource_id_2two"           }]}]} 

this fun exercise in recursive functions, should try work these things out though improve skills.

def sort_me(data)   if data.is_a? array     data.sort_by! { |h| h[:position] }     data.map { |i| sort_me(i) }   elsif data.has_key? :child     if data[:child].is_a? array       data[:child].map { |i| sort_me(i) }     end     return data[:child].sort_by! { |h| h[:position] }   elsif data.has_key? :child_child     return data[:child_child].sort_by! { |h| h[:position] }   end   return data end  puts sort_me(data[:parent]).to_yaml 

yaml results (easier eyes)

--- - :id: 29637484-4d39-4828-bebc-52e4ecb12250   :extra_data: parent_extra_data_1two   :position: 2   :resource_id: parent_resource_id_1two   :child:   - :id: 86b60506-f431-41f4-8555-30a83e9296f9     :extra_data: child_extra_data_1     :position: 1     :resource_id: child_resource_id_1     :child_child:     - :id: 04bb9af2-eac8-4fcf-8253-38b0ccb538c2       :extra_data: child_child_extra_data_one       :position: 1       :resource_id: child_child_one     - :id: 98fe0b96-a8cd-488b-bf00-9d48633355d3       :extra_data: child_child_extra_data_two       :position: 2       :resource_id: child_child_two     - :id: 94947cc4-e27a-4f79-b585-f26db7ce4e66       :extra_data: child_child_extra_data_three       :position: 3       :resource_id: child_child_three   - :id: 57c9dab3-f091-48e7-85a7-4a8a3c4782a6     :extra_data: child_extra_data_1two     :position: 2     :resource_id: child_resource_id_2two 

Comments

Popular posts from this blog

How to understand 2 main() functions after using uftrace to profile the C++ program? -

c# - Update a combobox from a presenter (MVP) -

How to put a lock and transaction on table using spring 4 or above using jdbcTemplate and annotations like @Transactional? -