Re: [xsl] Bug Sorting Zeros

Subject: Re: [xsl] Bug Sorting Zeros
From: Jeni Tennison <mail@xxxxxxxxxxxxxxxx>
Date: Mon, 25 Jun 2001 16:36:47 +0100
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:



<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="." />





Jeni Tennison

 XSL-List info and archive:

Current Thread