title: How tree.sort really works?
body: <p>I have the root node with the unsorted children nodes.<br>
  I use the <code>tree.nodes(treeData)</code> to get the nodes list and build a hierarchical list and I want it to be sorted. See example code on <a href="http://jsfiddle.net/qfKUh/1/" rel="nofollow">jsfiddle</a> or below:  </p>
  
  <pre class="lang-js prettyprint-override"><code>/// define data
  var treeData = {
      name: 'root',
      children: [
          { name: 1 },
          { name: 5 },
          { name: 2 },
          { name: 6 },
          { name: 4 },
          { name: 3, children: [ {name: 33 }, {name: 31 }, {name: 32 } ] },
          { name: 7 }
      ]
  };
  
  /// init tree
  var tree = d3.layout.tree()
      .sort(function(a, b) { return d3.ascending(a.name, b.name); });
  var nodes = tree.nodes(treeData); // nodes unsorted now
  // uncomment this to get sorted nodes
  // nodes = tree.nodes(treeData);
  
  /// build hierarchy
  var li = d3.select('ul').selectAll('li').data(nodes);
  li.enter().append('li')
      .html(function(d) {
          return Array(d.depth).join('∣  ') + (d.depth ? '↳ ' : '') + "&lt;b&gt;"+d.name+"&lt;/b&gt;" + " x: " + Math.round(d.x*100)/100 });
  </code></pre>
  
  <p>When <code>tree.sort()</code> is defined then <code>treeData.children</code> array is sorted but <code>tree.nodes(treeData)</code> returns the unsorted array of nodes.  </p>
  
  <p>Also I discovered that if I call <code>tree.nodes()</code> twice then it returns sorted nodes:  </p>
  
  <pre><code>nodes = tree.nodes(treeData) // returns unsorted: 1, 5, 2... but treeData.children is now sorted
  nodes = tree.nodes(treeData) // returns sorted: 1, 2, 3...
  </code></pre>
  
  <p>It seems that <code>tree.sort()</code> affects the <code>treeData.children</code> array and the <code>node.x</code> and <code>node.y</code> values but not does not affect the nodes order in the result of <code>tree.nodes()</code> function.  </p>
  
  <p><strong>So the question is what the proper way of using <code>tree.sort()</code> with <code>tree.nodes()</code>?</strong></p>