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

Michael Kay mike at saxonica.com
Thu Dec 3 05:10:17 PST 2015


As Rob points out, the simplest solution for simple cases is to use reverse().

That doesn't work if you want a composite sort key - sort by ascending country then descending city. (Yes, an unconvincing use case, and frankly it's because of the difficulty of finding a convincing use case that we didn't include this capability).

If you know that the result of collation-key() will be xs:base64Binary then you can do it, I suspect, by 

fn:sort($places, function($p) { $p!country, $p!city => collation-key() => bin:not() } )

where bin:not is from http://expath.org/spec/binary#not

Michael Kay
Saxonica 


> On 3 Dec 2015, at 12:44, Rob Stapper <r.stapper at lijbrandt.nl> wrote:
> 
> Hi Christian,
> 
> I suppose it can't be this simple: reverse( sort( ('A', 'AA', 'B')))
> 
> Rob Stapper
> 
> -----Oorspronkelijk bericht-----
> Van: talk-bounces at x-query.com [mailto:talk-bounces at x-query.com] Namens Christian Grün
> Verzonden: donderdag 3 december 2015 12:49
> Aan: talk at x-query.com
> Onderwerp: [xquery-talk] fn:sort: sort for strings in descending order
> 
> Hi everyone,
> 
> As you may know, XQuery 3.1 introduces an interesting new fn:sort function, which can be used as alternative for the order by clause in FLWOR expressions [1]. It can e.g. be used to sort numbers in a descending order:
> 
>  sort(
>    1 to 10,
>    function($k) { -$k }
>  )
> 
> I would be interested if anyone has already tried to get the descending order working for strings?
> 
>  sort(
>    ('A', 'AA', 'B'),
>    function($k) { ... }
>  )
> 
> I assume that string-to-codepoints could be used, and all Unicode points could then be negated, and additionally longer Unicode sequences would need to be preferred over shorter ones… But maybe there is a more concise solution?
> 
> Thanks in advance,
> Christian
> 
> [1] http://www.w3.org/TR/xpath-functions-31/#func-sort
> 
> _______________________________________________
> talk at x-query.com
> http://x-query.com/mailman/listinfo/talk
> 
> 
> ---
> Dit e-mailbericht is gecontroleerd op virussen met Avast antivirussoftware.
> http://www.avast.com
> 
> 
> _______________________________________________
> talk at x-query.com
> http://x-query.com/mailman/listinfo/talk





More information about the talk mailing list