|
[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message] Re: Bug Sorting Zeros
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
|
PURCHASE STYLUS STUDIO ONLINE TODAY!Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced! Download The World's Best XML IDE!Accelerate XML development with our award-winning XML IDE - Download a free trial today! Subscribe in XML format
|

Cart








