# Re: sort / unsort

 Subject: Re: sort / unsort From: Dimitre Novatchev Date: Fri, 9 Aug 2002 00:53:09 -0700 (PDT)
```--- "McKeever, Marty" <marty dot mckeever at bankofamerica dot com>
wrote:

> Interesting problem, i've been wracking my brain to solve.
>
> >From a group of nodes, output the first five in a sorted order, and
> dump the
> remainder in document order.
>
> that is:
> 	<foo>I</foo>
> 	<foo>F</foo>
> 	<foo>E</foo>
> 	<foo>D</foo>
> 	<foo>A</foo>
> 	<foo>C</foo>
> 	<foo>B</foo>
> 	<foo>H</foo>
> 	<foo>G</foo>
>
> would output:
> 	<foo>A</foo>
> 	<foo>B</foo>
> 	<foo>C</foo>
> 	<foo>D</foo>
> 	<foo>E</foo>
> 	<foo>I</foo>
> 	<foo>F</foo>
> 	<foo>H</foo>
> 	<foo>G</foo>
> note that only the first 5 are sorted.
> the remainder are in document order - less the 5 sorted items.
>
> TIA,
> Marty

Marty,

You can use one of my generic templates for partial sort:

http://www.topxml.com/code/default.asp?p=3&id=v20011001235959

its complexity is O(N*log2(k)), for finding the first k elements out of
N.

It doesn't produce a nodeset with copies of the k nodes -- just a
string containing the positions of these k nodes in the original
node-set.

This is also convenient for the second part of your problem -- just
copy all nodes, whose position() in the original node-set is not one of
the positions, contained in the result of the partial sort.

Note: Please, note that your problem only has meaning if it is
guaranteed that there are nore duplicate node values -- in case there
are, it is difficult to define what is "the first k nodes".

=====
Cheers,

Dimitre Novatchev.
http://fxsl.sourceforge.net/ -- the home of FXSL

__________________________________________________
Do You Yahoo!?
HotJobs - Search Thousands of New Jobs
http://www.hotjobs.com

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

```

### PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

### Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
 RSS 2.0 Atom 0.3