hclbaur / sdt-core

SDT Core Implementation
The Unlicense
0 stars 0 forks source link

The sort command #8

Closed hclbaur closed 3 months ago

hclbaur commented 4 months ago

The sort statement:

foreach "[expression]" {
        sort "[expression]" { 
            reverse "[expression]" comparator "[bifunction]" 
        } 
         ....
  }

The sort command will sort whatever node-list was selected by the foreach expression, prior to execution of the compound statement. Sort order can be reversed using the reverse keyword, for example: reverse "true()".

The type of sort (text/number) can be controlled using the comparator keyword, this needs some refinement though.

Originally posted by @hclbaur in https://github.com/hclbaur/sdt-core/issues/2#issuecomment-2116064481

hclbaur commented 3 months ago

The comparator will be an XPath expression that is evaluated during sorting, prior to the iteration. The objects to be compared in this expression are placeholders in this expression, for example:

    sort "[expression]" { 
        reverse "[expression]" comparator "some-function(?,?)" 
    }

The expression that is supplied must return < 0, 0 or 0 > depending on whether object 1 is smaller than, equal to or greater than object 2.

hclbaur commented 3 months ago

Need to implement: multiple SORT statements may be used to sort items first by one expression, then (when equal) by another, and another, etc:

foreach "[expression]" {
    sort "[expression 1]" { ... } 
    sort "[expression 2]" { ... } 
    sort "[expression 3]" { ... } 
     ....
}
hclbaur commented 3 months ago

Sorting text should also be done using a comparator, like sdt:compare-string() that uses collantion rules.

hclbaur commented 3 months ago

Available as of SDT 1.3.0.