[xquery-talk] fn:sort: sort for strings in descending order

Christian Grün christian.gruen at gmail.com
Fri Dec 4 02:52:44 PST 2015

Rob, Michael,

Thanks for your enlightening answers. The hint to fn:reverse was
helpful – I completely missed it – and I was not aware of the
punched-card technique (I overlooked that sort is stable).

One more use I case had in mind were top-k queries:

  fn:sort(...)[position() = 1 to 5]

>From the implementation point of view, such patterns can be sped up
with a min heap. I now realize that the heap can simply be replaced
with a max heap if an fn:sort call is wrapped by fn:reverse.


On Fri, Dec 4, 2015 at 11:21 AM, Michael Kay <mike at saxonica.com> wrote:
>> PS. The composite sort key is another tough cookie to crumble. I feel
>> challenged.
> Because fn:sort produces a stable sort, one can always go back to the old punched-card technique of doing multiple sorts, in minor-to-major key order:
> $places => sort(function($x){$x!city}) => reverse() => sort(function($x){$x!country})
> Michael Kay
> Saxonica

More information about the talk mailing list