title: Reconstruct or Rebuild existing JSON according to parent and item id
body: <p>I'm generate JSON array from table value and in table I'm storing Id, Parent_id and Data and some other value. Parent_id will be <code>NULL</code> if it doesn't have any parent, if yes then it will refer parent id.</p>
  
  <pre><code>[{"state":"open","data":"Signature","attr":
  {"id":0,"item_id":2,"parent_id":0},"children":[{"data":"or replace","attr":
  {"id":1,"item_id":3,"parent_id":2}}]},{"state":"open","data":"Parameter","attr":
  {"id":0,"item_id":4,"parent_id":0},"children":[{"data":"in parameter","attr":
  {"id":3,"item_id":5,"parent_id":4}},{"data":"in out parameter","attr":
  {"id":4,"item_id":6,"parent_id":4}}]},{"state":"open","data":"Mandatory Block","attr":
  {"id":0,"item_id":7,"parent_id":0},"children":[{"data":"validate Exit condition for loop","attr":{"id":8,"item_id":8,"parent_id":7}},{"data":"validate exception","attr":
  {"id":7,"item_id":9,"parent_id":7}}]},{"state":"open","data":"parent","attr":
  {"id":0,"item_id":10,"parent_id":7},"children":[{"data":"parent_CHILD","attr":
  {"id":2,"item_id":11,"parent_id":10}},{"data":"parent_CHILD1","attr":
  {"id":5,"item_id":12,"parent_id":10}}]}]
  </code></pre>
  
  <p>From above JSON, you can see <code>parent_id=0</code> means it has no parent. Now, I want to reconstruct it according to parent_id and Item_id, which means If <code>parent_id=0</code> then sort it according to item_id</p>
  
  <p>Help me to figure it out.</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>I tried following, but unable to get desire result.</p>
  
  <pre><code>&lt;script src='js/linq.js'&gt;&lt;/script&gt;
  &lt;script src='js/jquery.js'&gt;&lt;/script&gt;
  &lt;script&gt;
  
  
  var dataArray = $.parseJSON([{"state":"open","data":"Signature","attr":
  {"id":0,"item_id":2,"parent_id":0},"children":[{"data":"or replace","attr":
  {"id":1,"item_id":3,"parent_id":2}}]},{"state":"open","data":"Parameter","attr":
  {"id":0,"item_id":4,"parent_id":0},"children":[{"data":"in parameter","attr":
  {"id":3,"item_id":5,"parent_id":4}},{"data":"in out parameter","attr":
  {"id":4,"item_id":6,"parent_id":4}}]},{"state":"open","data":"Mandatory Block","attr":
  {"id":0,"item_id":7,"parent_id":0},"children":[{"data":"validate Exit condition for loop","attr":{"id":8,"item_id":8,"parent_id":7}},{"data":"validate exception","attr":
  {"id":7,"item_id":9,"parent_id":7}}]},{"state":"open","data":"parent","attr":
  {"id":0,"item_id":10,"parent_id":7},"children":[{"data":"parent_CHILD","attr":
  {"id":2,"item_id":11,"parent_id":10}},{"data":"parent_CHILD1","attr":
  {"id":5,"item_id":12,"parent_id":10}}]}]);
  var sorted = Enumerable.From(dataArray)
      .OrderBy(function(x) {
          return x.parent_id;
      })
      .ThenBy(function(x) {
          return x.item_id;
      })
      .ToArray();
  console.log(sorted);
  console.log(dataArray);
  &lt;/script&gt;
  </code></pre>