Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8531959
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T09:36:10+00:00 2026-06-11T09:36:10+00:00

I have to sort out the codes in numerical order. The codes have four

  • 0

I have to sort out the codes in numerical order.
The codes have four characters and four numerals.

for example,

COMP2100
COMP2400
COMP3410
LAWS2202
LAWS2250

when I just do <xsl:sort select="code" order="ascending" />
it displays above result.

However, I want that to be in ‘numerical order’ that is

COMP2100
LAWS2202
COMP2250
COMP2400
COMP3410

How do I do this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T09:36:12+00:00Added an answer on June 11, 2026 at 9:36 am

    Note: the OP has now provided sample XML. The below theories can be trivially adapted to this XML.

    I. XSLT 1.0 (part 1)

    Here is a simple solution that assumes your assertion (“the codes have four characters and four numerals”) will always be the case:

    <?xml version="1.0" encoding="utf-8"?>
    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="1.0">
      <xsl:output omit-xml-declaration="no" indent="yes" />
      <xsl:strip-space elements="*" />
    
      <xsl:variable name="vNums" select="'1234567890'" />
    
      <xsl:template match="node()|@*">
        <xsl:copy>
          <xsl:apply-templates select="node()|@*" />
        </xsl:copy>
      </xsl:template>
    
      <xsl:template match="/*">
        <t>
          <xsl:apply-templates>
            <xsl:sort select="substring(., 5)"
              data-type="number" />
          </xsl:apply-templates>
        </t>
      </xsl:template>
    </xsl:stylesheet> 
    

    …is applied to an imagined XML document, shuffled into random order:

    <?xml version="1.0" encoding="utf-8"?>
    <t>
      <i>COMP3410</i>
      <i>LAWS2202</i>
      <i>COMP2400</i>
      <i>COMP2100</i>
      <i>LAWS2250</i>
    </t>
    

    …the correct result is produced:

    <?xml version="1.0" encoding="utf-8"?>
    <t>
      <i>COMP2100</i>
      <i>LAWS2202</i>
      <i>LAWS2250</i>
      <i>COMP2400</i>
      <i>COMP3410</i>
    </t>
    

    Explanation:

    • The Identity Transform — one of the (if not the) most fundamental design patterns in XSLT — copies all nodes from the source XML document to the result XML document as-is.
    • One template overrides the Identity Transform by sorting all children of <t> based upon the characters in the string from position 5 to the string’s end.

    Again, note that this solution assumes your original assertion — “the codes have four characters and four numerals” — is (and always will be) true.


    II. XSLT 1.0 (part 2)

    A (potentially) safer solution would be to assume that there might be numerous non-numeric characters in various positions within the <i> nodes. In that case, this XSLT:

    <?xml version="1.0" encoding="utf-8"?>
    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="1.0">
      <xsl:output omit-xml-declaration="no" indent="yes" />
      <xsl:strip-space elements="*" />
    
      <xsl:variable name="vNums" select="'1234567890'" />
    
      <xsl:template match="node()|@*">
        <xsl:copy>
          <xsl:apply-templates select="node()|@*" />
        </xsl:copy>
      </xsl:template>
    
      <xsl:template match="/*">
        <t>
          <xsl:apply-templates>
            <xsl:sort select="translate(., translate(., $vNums, ''), '')"
              data-type="number" />
          </xsl:apply-templates>
        </t>
      </xsl:template>
    </xsl:stylesheet>
    

    …provides the same result:

    <?xml version="1.0" encoding="utf-8"?>
    <t>
      <i>COMP2100</i>
      <i>LAWS2202</i>
      <i>LAWS2250</i>
      <i>COMP2400</i>
      <i>COMP3410</i>
    </t>
    

    Explanation:

    • The Identity Transform is once again used.
    • In this case, the additional template uses the so-called Double Translate Method (first proposed by Michael Kay and first shown to me by Dimitre Novatchev) to remove all non-numeric characters from the value of each <i> element before sorting.

    III. XSLT 2.0 Solution

    Here’s a possible XSLT 2.0 solution is very similar to part 2 of the XSLT 1.0 solution; it merely replaces the Double Translate Method with XPath 2.0’s ability to handle regular expressions:

    <xsl:sort select="replace(., '[^\d]', '')" data-type="number" />
    

    Note that by no means are you required to use regular expressions in XPath 2.0; the Double Translate Method works just as well as in XPath 1.0. The replace() method will, however, most likely be more efficient.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have to sort out my data by some column, such that some specific
I am just facing trouble to sort out the query I need. I have
I have a simple question, not able to sort it out. I have a
I am trying to create a sort of slideshow animation. I have the codes
Can someone help me sort out this compiler error? I have a class like
Just need help as I have been trying sort this out for ages now.
We have an interesting scenario I need to sort out: 1) We have an
I have a perplexing problem in trying to sort out different behavior of the
I have code that when given a thing it needs to sort out what
I have to be able to sort out a list of object which might

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.