Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Claushingebjerg 936 posts 2571 karma points
    Aug 23, 2012 @ 14:14
    Claushingebjerg
    0

    Sorting alphabetically first numeric second?

    I need to sort a set of nodes alphabetically first numeric second...

    I use the standard

    <xsl:sort select="@nodeName" order="ascending" />
    

    this out putsnodes in this form:
    1-2-3-a-b-c


    What i need to output is:
    a-b-c-1-2-3

    Any ideas how to achieve this with xslt?

     

  • Chriztian Steinmeier 2798 posts 8788 karma points MVP 7x admin c-trib
    Aug 23, 2012 @ 14:18
    Chriztian Steinmeier
    0

    Hi Claus,

    This could be really tricky or really easy, depending on the exact data - how much can you share about the actual nodeNames? Are they just like that, or could they be anything?

    /Chriztian

  • Chriztian Steinmeier 2798 posts 8788 karma points MVP 7x admin c-trib
    Aug 23, 2012 @ 14:28
    Chriztian Steinmeier
    1

    The trick to this is that you can use more than one <sort> statement - so you could have the first be an expression that resolve to a boolean, to determine if the current nodeName is numeric or textual, e.g.:

    <xsl:sort select="number(@nodeName = number(@nodeName))" data-type="number" order="descending" />

    This would return either 1 or 0 (because number(@nodeName) will return 'NaN' for any non-numeric string) 

    But that will only work if the nodeNames to treat as numeric are "clean" numbers...

    /Chriztian

  • Claushingebjerg 936 posts 2571 karma points
    Aug 23, 2012 @ 14:31
    Claushingebjerg
    0

    The are not clean numbers :(

    They are like "1-form", "50/50" and like that...

  • Chriztian Steinmeier 2798 posts 8788 karma points MVP 7x admin c-trib
    Aug 23, 2012 @ 14:59
    Chriztian Steinmeier
    1

    Hmm - Then the only thing I can think of right now, would be to use the translate() function to change the digits 0-9 into characters higher in the Unicode set (there's a range reserved for "private use" - cool, right :-)

    Try this sort statement: 

    <xsl:sort select="translate(@nodeName, '0123456789', '&#xE000;&#xE001;&#xE002;&#xE003;&#xE004;&#xE005;&#xE006;&#xE007;&#xE008;&#xE009;')" data-type="text" order="ascending" />

     

    /Chriztian

  • Claushingebjerg 936 posts 2571 karma points
    Aug 23, 2012 @ 15:35
    Claushingebjerg
    0

    Wow, that was simple :) i was getting ready for somethin really advanced :) 

Please Sign in or register to post replies

Write your reply to:

Draft