[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: Bug Sorting Zeros

Subject: Re: Bug Sorting Zeros
From: Jeni Tennison <mail@xxxxxxxxxxxxxxxx>
Date: Mon, 25 Jun 2001 16:36:47 +0100
xsl sort numeric order
David Marston wrote:
> Rechell Schwartz writes:
>>Similarly, when sorting strings is there a way to cause numerical
>>values to appear after all string values instead of before?
>
> If your data type is "text", numbers get no special recognition. It
> appears that you want to do a two-level sort, where the upper level
> is a boolean on numeric vs. non-numeric (likely inefficient to
> calculate) and the lower sort is numeric. If you also need the text
> strings to be alphabetically ordered at the same time that the
> numbers are sorted, you'll have to look at it as a two-pass
> situation, possibly using template modes.

You don't have to go to a two-pass solution unless you want to - you
can do a three-level sort:

  * boolean sort on whether the value is a number
    (get all the numbers to come after the string values)
    
  * numerical sort
    (sort the numbers in numerical order, strings all sort as NaN
     so their order doesn't change)
     
  * text sort
    (sort the strings in alphabetical order; numerical order won't
     change unless you have numbers with leading zeros, in which
     case they'll be put before their equivalent number without)

Here's an example source:

<values>
   <val>5</val>
   <val>foo</val>
   <val>0</val>
   <val>31</val>
   <val>bar</val>
   <val>031</val>
</values>

Template:

<xsl:template match="values">
   <xsl:for-each select="val">
      <xsl:sort select="number() or number() = 0" />
      <xsl:sort select="." data-type="number" />
      <xsl:sort select="." />
      <xsl:copy-of select="." />
   </xsl:for-each>
</xsl:template>

Result:

<val>bar</val>
<val>foo</val>
<val>0</val>
<val>5</val>
<val>031</val>
<val>31</val>

Cheers,

Jeni

---
Jeni Tennison
http://www.jenitennison.com/


 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.