title: Sort nested lists in r
body: <p>I have several lists with N objects each (like <code>unsorted$b</code> below) and a data frame with N rows (<code>unsorted$a</code> below). All of these are in a single list (<code>unsorted</code>). I wish to sort the data frame, while re-arranging every nested list to preserve the general order. For example:</p>
  
  <pre><code>&gt; unsorted &lt;- list(a=data.frame(age=c(30,10,20), name=c("ann","bob","carl")), b=list("3x10","1x10","2x10"))
  &gt; unsorted
  $a
    age name
  1  30  ann
  2  10  bob
  3  20 carl
  
  $b
  $b[[1]]
  [1] "3x10"
  
  $b[[2]]
  [1] "1x10"
  
  $b[[3]]
  [1] "2x10"
  </code></pre>
  
  <p>I wish to sort by age</p>
  
  <pre><code>ord &lt;- order(unsorted$a$age)
  </code></pre>
  
  <p>and this does the trick:</p>
  
  <pre><code>sorted &lt;- list(a=unsorted$a[ord,], b=unsorted$b[ord])
  &gt; sorted
  $a
    age name
  2  10  bob
  3  20 carl
  1  30  ann
  
  $b
  $b[[1]]
  [1] "1x10"
  
  $b[[2]]
  [1] "2x10"
  
  $b[[3]]
  [1] "3x10"
  </code></pre>
  
  <p>Can this be done with apply or one of its incarnations?</p>
  
  <p><strong>-- EDIT --</strong></p>
  
  <p>The parent list in my real case has a large number of sublists. All sublists contain N objects, but these objects have variable structures. </p>