title: SQL Server : FOR XML sorting control by attribute
body: <p>I am generating a XML file from a SQL Server query.</p>
  
  <p>I have some problem about sorting elements issues.</p>
  
  <p>For example, there is simple code as below.</p>
  
  <pre><code>&lt;test&gt;
      &lt;tree abc="123"/&gt;
      &lt;tree abc="789"/&gt;
      &lt;tree-order abc="456"/&gt;
  &lt;/test&gt;
  </code></pre>
  
  <p>As you can see tree and tree-order are different elements and I want to sort by attributes as</p>
  
  <pre><code>&lt;test&gt;
      &lt;tree abc="123"/&gt;
      &lt;tree-order abc="456"/&gt;
      &lt;tree abc="789"/&gt;
  &lt;/test&gt;
  </code></pre>
  
  <p>like this.</p>
  
  <p>Anyone has good idea to solve this problem?</p>
  
  <p>Thank you.</p>
  
  <p>Sorry I have detail question as below</p>
  
  <p>AS is </p>
  
  <pre><code> &lt;Main&gt;
  
  &lt;test&gt;
      &lt;tree abc="123"/&gt;
      &lt;tree abc="789"/&gt;
      &lt;tree-order abc="456"/&gt;
  &lt;/test&gt;
  
  &lt;test&gt;
      &lt;tree abc="123"/&gt;
      &lt;tree abc="789"/&gt;
      &lt;tree-order abc="456"/&gt;
  &lt;/test&gt;
  
  &lt;/Main&gt;
  </code></pre>
  
  <p>To be : </p>
  
  <pre><code>&lt;Main&gt;
  
  &lt;test&gt;
      &lt;tree abc="123"/&gt;
      &lt;tree abc="456"/&gt;
      &lt;tree-order abc="789/&gt;
  &lt;/test&gt;
  
  &lt;test&gt;
      &lt;tree abc="123"/&gt;
      &lt;tree abc="456"/&gt;
      &lt;tree-order abc="789/&gt;
  &lt;/test&gt;
  
  &lt;/Main&gt;
  </code></pre>
  
  <p>Thank you.</p>