title: Controlling the sibling order under recursive CTE?
body: <p>I have a CTE query which looks for main leafs and sub leafs. but I'm having trouble controling the leaf selection order <strong>between 2 siblings</strong> :</p>
  <p>Each row in the table is declared as :</p>
  <p><code>(childID INT ,parentID INT ,NAME NVARCHAR(30),location int)</code></p>
  <p>Where <code>location</code> is a priority to sort IFF they are siblings.</p>
  <p>And so  I have this tree structure :  those pairs has a location priority :</p>
  <p><img src="https://i.stack.imgur.com/j3Cz8.png" alt="enter image description here" /></p>
  <p>For example :</p>
  <pre><code>`a` ( location=1) should be before `f` (location=2)
  `b` ( location=1) should be before `e` (location=2)
  `d` ( location=1) should be **before** `c` (location=2)
  </code></pre>
  <p>The problem is that it seems that I must <strong>first</strong> <code>order by</code> <code>childID</code> in order to see the right  structure ( <em>sibling unsorted</em>).</p>
  <p>But  - what how does my <code>order by</code> should look like so I will be able to see the right structure   (<strong>&amp;&amp; sibling sorted</strong>) ?</p>
  <p><em>(in my example : <code>d</code> should come before <code>c</code>)</em></p>
  <p><a href="https://data.stackexchange.com/stackoverflow/query/edit/127231" rel="nofollow noreferrer"><strong>Here is the working query which yields all the tree leafs ( unsorted siblings)</strong></a></p>
  <p>p.s. <code>childID</code> is <strong>not</strong> indicating anything about the sorting. it's just a placeholder. as I said , the location between 2 brothers is by the <code>location</code> column.( here  , childId is sorted because thats the order of which i inserted the rows...</p>