title: Why doesn't xsl:sort work when using xsl:key?
body: <p>Given this XML:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8" ?&gt;
  &lt;data&gt;
      &lt;chapter-section-datasource&gt;
          &lt;section id="12" handle="chapter-section"&gt;Chapter Section&lt;/section&gt;
          &lt;entry id="94"&gt;
              &lt;order handle="1"&gt;1&lt;/order&gt;
          &lt;/entry&gt;
      &lt;/chapter-section-datasource&gt;
      &lt;page-content&gt;
          &lt;section id="9" handle="page-content"&gt;x&lt;/section&gt;
          &lt;chapter-section link-id="94"&gt;
              &lt;entry id="87"&gt;
                  &lt;section-id&gt;0&lt;/section-id&gt;
              &lt;/entry&gt;
              &lt;entry id="91"&gt;
                  &lt;section-id&gt;2&lt;/section-id&gt;
              &lt;/entry&gt;
              &lt;entry id="93"&gt;
                  &lt;section-id&gt;1&lt;/section-id&gt;
              &lt;/entry&gt;
              &lt;entry id="103"&gt;
                  &lt;section-id&gt;3&lt;/section-id&gt;
              &lt;/entry&gt;
          &lt;/chapter-section&gt;
      &lt;/page-content&gt;
  &lt;/data&gt;
  </code></pre>
  
  <p>And this XSL:</p>
  
  <pre><code>&lt;xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;
  
    &lt;xsl:template match="data"&gt;
      &lt;xsl:apply-templates select="chapter-section-datasource/entry[1]" mode="bal"/&gt;
    &lt;/xsl:template&gt;
  
    &lt;!-- Map Results --&gt;
    &lt;xsl:key name="guide" match="chapter-section" use="@link-id" /&gt;
  
    &lt;xsl:template match="entry" mode="bal"&gt;
      &lt;xsl:apply-templates select="key('guide', @id)" mode="balGuide"&gt;
        &lt;xsl:sort select="section-id" data-type="number" order="ascending" /&gt;
      &lt;/xsl:apply-templates&gt;
    &lt;/xsl:template&gt;
  
    &lt;xsl:template match="chapter-section/entry" mode="balGuide"&gt;
      &lt;xsl:element name="div"&gt;
            &lt;xsl:value-of select="section-id"/&gt;
      &lt;/xsl:element&gt;
    &lt;/xsl:template&gt;
  
  &lt;/xsl:stylesheet&gt;
  </code></pre>
  
  <p>The expected output is: <strong>0,1,2,3</strong></p>
  
  <p>The actual output is: <strong>0,2,1,3</strong></p>
  
  <p>Why isn't sort working as I expect? Note that the complexity of the transform is due the more complex XML and XSL which has been simplified for this example.</p>
  
  <p>If it matters, the transformation is done in C#</p>
  
  <pre><code>System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
  doc.Load(Server.MapPath("Xml/simple.xml"));
  System.Xml.Xsl.XslTransform trans = new System.Xml.Xsl.XslTransform();
  trans.Load(Server.MapPath("Xml/simple.xsl"));
  Xml1.Document = doc;
  Xml1.Transform = trans;
  </code></pre>