RE: [xsl] Tokenized String Sorting Puzzle/Problem

Subject: RE: [xsl] Tokenized String Sorting Puzzle/Problem
From: "Scott Lynch" <slynch@xxxxxxxxxx>
Date: Wed, 26 Mar 2008 13:02:30 -0400
A big THANKS to everyone who responded! It's terrible when something so
simple is right in front of me (smacking my forehead with my palm at
this moment..). The double sort statements was the missing part of all
my failed attempts. I was trying to nest two for-each loops with
individual sort statements, with bad results. The double sort solved it
nicely.

You folks rock!

cheers,
Scott

-----Original Message-----
From: Lynch, Scott (GWRTP:3798)
Sent: Wednesday, March 26, 2008 9:18 AM
To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
Subject: [xsl] Tokenized String Sorting Puzzle/Problem


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

Current Thread