xsl-list
[Top] [All Lists]

RE: Subject: Sorting alpha numeric values

2003-05-09 01:38:47


De : Hugh Dixon [mailto:hugh(_dot_)dixon(_at_)listech(_dot_)com(_dot_)au]

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



<Prev in Thread] Current Thread [Next in Thread>