QM provides sorting options in the query processor and in the QMBasic programming language.
There are four styles of sorting available:
Sorting in the Query Processor
The query processor provides four sort clause options; BY, BY.DSND, BY.EXP and BY.EXP.DSND. The first two sort simple data into ascending or descending order respectively. The second two work on exploded data from multivalued data items. The sort operation will be performed in left aligned style unless the field being sorted is defined as right aligned in which case a compound sort is used.
Sorting in Alternate Key Indices
Alternate key indices sort the indexed values using a compound sort. Within any index entry, record ids are sorted using either left or right aligned sort mode depending on the justification specified in the dictionary.
Sorting in QMBasic
QMBasic provides two methods of sorting. The !SORT() subroutine sorts the items in a dynamic array based on a sort style and other options specified using a sort rule parameter. This also allows removal of duplicate items, case insensitivity, choice of ascending or descending sequence and omission of null items.
The second sort method uses the concept of a sort session in which a sort is initialised using the SORT() function which allows up to up to 32 keys for nested sorting criteria plus an optional data element. Items are added to the sort using the SORTADD statement. Once all the data has been added, items may be extracted individually with the SORTNEXT() function or as a field mark delimited dynamic array using the SORTDATA() function.
It is valid for a single QM process to have multiple sorts active simultaneously though this can lead to high memory and disk space usage. It is recommended that sort sessions that are no longer required are terminated using SORTCLEAR. A sort session is automatically terminated if the sort variable returned by SORT() is overwritten or discarded.
Sorting in ECS Mode Systems
In a non-ECS (8-bit character) system, the collating sequence is simply the numeric value of each character. This does not allow, for example, the French é (character 233) to appear next to its unaccented equivalent.
In an ECS system, the collating sequence is defined by the character map. It is valid for different users of the same application to have different maps and hence see sorted data in a different order. Character maps for common languages can be downloaded from the openqm.com web site. Changes can be applied to a map using the EDIT.MAP command.