The Sort Clause

Top  Previous  Next

 

The optional sort clause determines the order in which records are inserted into the select list (SELECT, SSELECT, SEARCH) or reported (LIST, SORT, LIST.ITEM, SORT.ITEM).

 

Sorting is performed before conversion of data to its external format. Thus sorts of date fields, for example, will correctly sequence dates regardless of their conversion.

 

The justification mode of the field's format is used to determine whether a left of right aligned sort is performed. For dates, a right aligned sort is required to avoid problems with dates with internal values of differing numbers of digits.

 

There are two sort clause operators for single valued fields, BY and BY.DSND, which differ only in that BY sorts into ascending order and BY.DSND sorts into descending order. Similarly, there are two exploded sort operators, BY.EXP and BY.EXP.DSND, for use with multi-valued fields. These explode the multi-valued records to their single valued equivalents, allowing a query to process values in sequence.

 

Where multiple sort items are specified, the query processor examines them in the order in which they appear in the command. The second and subsequent sort items are only examined where the previous sort was not sufficient to identify the record sequence.

 

The SORT, SORT.ITEM and SSELECT verbs are equivalent to the LIST, LIST.ITEM and SELECT verbs with a BY @ID clause as the final sort.