Sorting

In both Example 42.5, and Example 42.6, the two selection examples above, we applied sorting to the xml data. Sorting may be done with multiple sort criteria as in

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()" />
        </xsl:copy>
    </xsl:template>
    <xsl:template match="feed">
        <xsl:copy>
            <xsl:apply-templates>
                <xsl:sort select="updated" order="descending"/>
                <xsl:sort select="title"/>
                <xsl:sort select="id" data-type="number"/>
            </xsl:apply-templates>
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>

Apply this to

<feed>
  <entry>
    <id>4</id>
    <updated>2011-01-18T16:55:54Z</updated>
    <title>title2</title>
  </entry>
  <entry>
    <id>3</id>
    <updated>2011-01-18T16:55:53Z</updated>
    <title>title1</title>
  </entry>
  <entry>
    <id>2</id>
    <updated>2011-01-18T16:55:54Z</updated>
    <title>title1</title>
  </entry>
  <entry>
    <id>5</id>
    <updated>2011-01-18T16:55:54Z</updated>
    <title>title</title>
  </entry>
</feed>

Code adapted from https://stackoverflow.com/questions/4727816/xslt-sort-by-multiple-items