RE: Subject: [xsl] Sorting alpha numeric values

Subject: RE: Subject: [xsl] Sorting alpha numeric values
From: David Neary <David@xxxxxxxxx>
Date: Fri, 9 May 2003 10:38:47 +0200

De : Hugh Dixon [mailto:hugh.dixon@xxxxxxxxxxxxxx]
> 
> I want to sort a data set:
[snip]

> Into:
> A1
> A3George
> A21Fred
> A21Value
> B3Value
> B10Valuf
> 
> Sorting by the number and alpha prefix is essential, sorting by any
> suffix of numbers and alphas would be 'icing on the cake'.

Ah - but the same solution will work - use 3 sort keys.

1) initial alpha:

<xsl:sort select="substring(.,1,1)" data-type="text"/>

2) Numeric data (note: this translation assumes that there's no numeric data
in the trailing text - A12Bad12 will translate to 1212, which is not what
you want)

<xsl:sort
select="translate(.,'0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRS
TUVWXYZ', '0123456789')" data-type="number")/>

3) Trailing alpha data (This one relies on the fact that since we've already
sorted on the first letter, we can include that data in the sort string
without changing the desired order).

<xsl:sort
select="translate(.,'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ012
3456789', 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ')"
data-type="text")/>

Et voilà, as they say here.

Cheers,
Dave.

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


Current Thread