Water 5-System Interfaces-Database
method sort
Contract
Return typewob
Parameter keyDefault valueType
ordering_methodobject_less_or_equalvar
Water Contract
<method sort
  ordering_method=object_less_or_equal/>

See also: is_sorted

Expects _subject to be a vector. Elements are sorted according to predicate (a method that returns true or false). _subject is modified and returned with elements in the sorted order. predicate is optional. It is a method that compares _subject and its first argument returning true if _subject should be before the first argument in the sorted order. The default value is object_less, also called ascending which sorts all numbers first in ascending order, then all other objects in alphabetical order when they are printed using to_html.
<vector "aab" 347 "aaa" 346/>.<sort/>
<vector 346 347 "aaa" "aab"/>

Another common method to pass sort is 'descending' which will produce a sequence in the opposite order from the method 'ascending'. If you write your own method for the sort predicate, it should take a subject and one argument. It should return true if the subject is to come before the argument in the resulting order. Note that the predicate should return false if its subject and argument are considered to be "equal" for the sorting order. For example:
<method order_by_x obj=req>  obj.x.<more_or_equal .x/> </>
<vector <thing x=100/> <thing x=20/> <thing x=10/> />.
  <sort order_by_x />
<vector <thing x=10/> <thing x=20/> <thing x=100/>/>

Sort can be used to sort by more than one key by making a sort method that looks at more than one key. Here's an example:
<vector <thing a="f" x=0/> <thing a="f" x=1/>
        <thing a="g" x=1/> <thing a="g" x=0/>
        <thing a="g" x=3/> <thing a="g" x=2/>
/>.
   <sort
  <method sort_by_a_then_x obj=req>
   <if> .a.<equal obj.a/>
        .x.<object_less obj.x/> <!-- sort by 2nd key -->
     else
        .a.<object_less obj.a/> <!-- sort by primary key -->
   </if>
  </method>
   />
<vector <thing x=0 a="f"/> <thing x=1 a="f"/> <thing x=0 a="g"/> <thing x=1 a="g"/> <thing x=2 a="g"/> <thing x=3 a="g"/>/>