XSLT <sort> Element Last Updated : 28 Apr, 2025 Comments Improve Suggest changes Like Article Like Report XSLT <sort> Element is used to sort the node which is selected. The XSLT <sort> element is added inside the <xsl:for-each> element. Syntax: <xsl:sort select= node order="ascending" | "descending" case-order="upper-first" | "lower-first" lang=XML:LANG-CODE data-type="text" | "number" />Parameters: select: It is used to select a node with which we sort the node. order: It specifies the ordered sequence.case-order: It specifies the order in which upper case and lower case letters are considered first. lang: It specifies language is to be used by the sort. data-type: It defines which is first sorted alphabets or numbers. Example 1: In this example, we will sort the element of XML with the help of the age node. Save Both files as mentioned and open xsl file on the Browser, it will show the output as shown below: XML <!--File: Test.xml--> <?xml version="1.0" encoding="UTF-8"?> <?xml-stylesheet type="text/xsl "href="Rule.xsl" ?> <student> <s> <name> Divyank Singh Sikarwar </name> <branch>CE</branch> <age>18</age> <city> Agra </city> </s> <s> <name> Aniket Chauhan </name> <branch> CSE</branch> <age>20</age> <city> Shahjahanpur </city> </s> <s> <name> Simran Agarwal</name> <branch> CSE</branch> <age>23</age> <city> Buland Shar</city> </s> <s> <name> Abhay Chauhan</name> <branch>ME</branch> <age>17</age> <city> Shahjahanpur</city> </s> <s> <name> Himanshu Bhatia</name> <branch>IT</branch> <age>25</age> <city> Indore</city> </s> </student> XML <!--File: Rule.xsl--> <?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <html> <body> <h1 align="center">Students' Basic Details</h1> <table border="3" align="center" > <tr> <th>Details</th> </tr> <xsl:for-each select="student/s"> <xsl:sort select="age"/> <tr> <td> <xsl:apply-templates select="name"/> <xsl:apply-templates select="branch"/> <xsl:apply-templates select="age"/> </td> </tr> </xsl:for-each> </table> </body> </html> </xsl:template> <xsl:template match="name"> Name: <span style="font-family:cursive;color:#ff0000"> <xsl:value-of select="."/> </span> <br /> </xsl:template> <xsl:template match="branch"> Branch: <span style="font-family:serif;color:#0ff000"> <xsl:value-of select="."/> </span> <br /> </xsl:template> <xsl:template match="age"> Age: <span style="font-family:fantsy;color:#0000ff"> <xsl:value-of select="."/> </span> <br /> </xsl:template> </xsl:stylesheet> Output: sort Example 2: In this example, we will sort the elements of XML in descending order with name node. Save Both files as mentioned and open xsl file on the Browser, it will show the output as shown below: XML <!--File: Test.xml--> <?xml version="1.0" encoding="UTF-8"?> <?xml-stylesheet type="text/xsl "href="Rule.xsl" ?> <student> <s> <name> Divyank Singh Sikarwar </name> <branch> CSE</branch> <age>18</age> <city> Agra </city> </s> <s> <name> Aniket Chauhan </name> <branch> CSE</branch> <age> 20</age> <city> Shahjahanpur </city> </s> <s> <name> Simran Agarwal</name> <branch> CSE</branch> <age> 23</age> <city> Buland Shar</city> </s> <s> <name> Abhay Chauhan</name> <branch> CSE</branch> <age> 17</age> <city> Shahjahanpur</city> </s> <s> <name> Himanshu Bhatia</name> <branch> IT</branch> <age> 25</age> <city> Indore</city> </s> </student> XML <!--File: Rule.xsl--> <?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <html> <body> <h1 align="center">Students' Basic Details</h1> <table border="3" align="center" > <tr> <th>Name</th> <th>Branch</th> <th>Age</th> </tr> <xsl:for-each select="student/s"> <xsl:sort select="name" order="descending"/> <tr> <td> <xsl:apply-templates select="name"/> </td> <td> <xsl:apply-templates select="branch"/> </td> <td> <xsl:apply-templates select="age"/> </td> </tr> </xsl:for-each> </table> </body> </html> </xsl:template> <xsl:template match="name"> <span style="color:#ff0000"> <xsl:value-of select="."/> </span> <br /> </xsl:template> <xsl:template match="branch"> <span style="color:#0ff000"> <xsl:value-of select="."/> </span> <br /> </xsl:template> <xsl:template match="age"> <span style="color:#0000ff"> <xsl:value-of select="."/> </span> <br /> </xsl:template> </xsl:stylesheet> Output: sort Comment More infoAdvertise with us Next Article How to sort a list in C# | List.Sort() Method Set -1 S singhanuj78276 Follow Improve Article Tags : HTML Web technologies-HTML and XML Geeks Premier League 2023 Similar Reads How to sort HTML elements by data-attribute ? In this article let us see how we can sort HTML elements using data-attribute.We can sort the HTML elements like lists or any other using the data attribute. The data attribute consists of 2 parts which are the prefix and suffix, the prefix data- is compulsory and the suffix of data- can be any cust 3 min read Excel SORT Function Explore the power of your Excel data with our easy-to-follow guide on Excel Sort Function. Whether youâre a beginner or an experienced professional, learning Excelâs sorting and filtering tools can enhance your workflow and provide valuable insights. This article explores each step, ensuring you can 6 min read What is Sorting in DSA | Sorting meaning Sorting is defined as the process of arranging a collection of data elements in a specific order, usually in ascending or descending order based on a specific attribute of the data elements. Characteristics of Sorting:Time Complexity: Time complexity, a measure of how long it takes to run an algorit 3 min read How to sort a list in C# | List.Sort() Method Set -1 List<T>.Sort() Method is used to sort the elements or a portion of the elements in the List<T> using either the specified or default IComparer<T> implementation or a provided Comparison<T> delegate to compare list elements. There are total 4 methods in the overload list of th 5 min read How to sort a list in C# | List.Sort() Method Set -1 List<T>.Sort() Method is used to sort the elements or a portion of the elements in the List<T> using either the specified or default IComparer<T> implementation or a provided Comparison<T> delegate to compare list elements. There are total 4 methods in the overload list of th 5 min read How to set list in descending order in HTML5 ? HTML Lists are used to create informative lists. Any list will have one or more list items. We have three types of lists in HTML. ul: An unordered list is referred to as an ul. Simple bullets would be used to list the items.ol: A number that is arranged in a certain order. This will list the items u 2 min read Like