title: Ruby sort array of hashes by child-parent relation
body: <p>So we have and array of hashes</p>
  
  <pre><code>array = [
    {id: 1, parent_id: 0},
    {id: 2, parent_id: 1},
    {id: 3, parent_id: 0},
    {id: 4, parent_id: 2}
  ]
  target_array = []
  </code></pre>
  
  <p>What is the most efficient and ruby way to map/sort that array to the following result:  </p>
  
  <pre><code>target_array = [
    {id:1,children:
      [{id: 2, children: [
        {id:4, children:[]}]}]},
    {id: 3, children:[]}  
  ]
  </code></pre>
  
  <p>p.s.The most I am capable of is iterating whole thing for each item and excluding from array hash that is already mapped to <code>target_array</code>.</p>