xsl-list
[Top] [All Lists]

[xsl] Tokenized String Sorting Puzzle/Problem

2008-03-26 06:19:17

I've been attempting to write a template which sorts a tokenized list of
pairs of numbers with little success.

Here's what I've been attempting to do:

Given a string like:
"1 0 2 1 1 2 1 3 1 4 2 0 1 1 2 3 2 4 6 0 5 0 10 0 10 1 10 2"

I want to tokenize and build pairs like:

"1 0", "2 1", "1 2", "1 3", "1 4", "2 0", "1 1", "2 3", "2 4", "6 0", "5
0", "10 0", "10 1", "10 2"

That part I can do (and have done) fairly easily using a simple for-each
loop. 

What I can't seem to accomplish is sorting the resulting pairs into
numerical order based on the first and then the second values in each
pair. Like this (this line breaks are just for visual clarity in this
email and are not necessary in the transform):

"1 0", "1 1", "1 2", "1 3", "1 4", 
"2 0", "2 1", "2 3", "2 4", 
"5 0", 
"6 0", 
"10 0", "10 1", "10 2"

I have a solution to this which uses a recursive template to walk the
list of tokens and build a duplicate sorted list, but it's not efficient
(i.e. O(n^2) at minimum). If there is a simple, elegant, efficient
solution to this puzzle, I sure would like to see it. My head is sore
from the constant banging on my keyboard...

thanks,
Scott

--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--