The SORT TABLE feature can be used to sort the elements of a table.
General Format
Syntax Rules
- Data-name-1 is a KEY data-name and is subject to the following rules:
- KEY data-names can be qualified.
- The data items identified by KEY data-names must not be variable length items.
- KEY data items can be floating-point items.
- If the KEY is an external floating-point item, the Compiler treats the data item as character data, rather than numeric data.
The sequence in which the records are sorted depends on the collating sequence used.
- If the KEY data item is internal floating-point, the records are sorted in numeric order of key values.
- Data-name-2 can be qualified and must have an OCCURS clause in the data description entry.
- The data item referenced by data-name-1 must be the same as the data item referenced by data-name-2, or an entry subordinate
to the data item referenced by data-name-2.
- Unless data-name-1 and data-name-2 are the same, the data item referenced by data-name-1 must not be described by an entry
containing an OCCURS clause. The data item referenced by data-name-1 must not be subordinate to an entry containing an OCCURS
clause that is also subordinate to data-name-2.
- The KEY phrase can be omitted only if the description of the table referenced by data-name-2 contains a KEY phrase.
- If data-name-2 references a data item subordinate to a data item that contains an OCCURS clause, an index-name must be associated
with each data item containing an OCCURS clause to which the data item referenced by data-name-2 is subordinate.
General Rules
- The words ASCENDING and DESCENDING are transitive across all occurrences of data-name-1 until another word ASCENDING or DESCENDING
is encountered.
- The data items referenced by the specifications of data-name-1 are the key data items which determine the order in which records
are returned from the file referenced by file-name-1 or the order in which the table elements are stored after sorting takes
place. The order of significance of the keys is the order in which they are specified in the SORT statement, without regard
to their association with ASCENDING or DESCENDING phrases.
- If the DUPLICATES phrase is specified and the contents of all the key data items associated with one data record or table
element are equal to the contents of the corresponding key data items associated with one or more other data records or table
elements, the order of return of these records is:
- The order of the associated input files as specified in the SORT statement, or by means of an RTS switch. Within a given input
file the order is that in which the records are accessed from that file.
- The order in which these records are released by an input procedure, when an input procedure is specified.
- The relative order of the contents of these table elements before sorting takes place.
- If the DUPLICATES phrase is not specified and the contents of all the key data items associated with one data record or table
element are equal to the contents of the corresponding key data items associated with one or more other data records or table
elements, the order of return of these records or the relative order of the contents of these table elements is undefined.
- The collating sequence that applies to the comparison of the nonnumeric key data items specified is determined at the beginning
of the execution of the SORT statement in the following order of precedence:
- First, the collating sequence established by the COLLATING SEQUENCE phrase, if specified, in the SORT statement.
- Second, the collating sequence established as the program collating sequence.
- The SORT statement sorts the table referenced by data-name-2 and presents the sorted table in data-name-2 either in the order
determined by the ASCENDING or DESCENDING phrases, if specified, or in the order determined by the KEY phrase associated with
data-name-2.
- To determine the relative order in which the table elements are stored after sorting, the contents of the corresponding key
data items are compared according to the rules for comparison of operands in a relation condition, starting with the most
significant key data item.
- If the contents of the corresponding key data items are not equal and the key is associated with the ASCENDING phrase, the
table element containing the key data item with the lower value has the lowest occurrence number.
- If the contents of the corresponding key data items are not equal and the key is associated with the DESCENDING phrase, the
table element containing the key data item with the higher value has the lowest occurrence number.
- If the contents of the corresponding key data items are equal, the determination is based on the contents of the next most
significant key data item.
- The number of occurrences of table elements referenced by data-name-2 is determined by the rules in the OCCURS clause.
- If data-name-2 references a data item which is subordinate to a data item which contains an OCCURS clause, the first or only
index-name associated with each such superordinate table must be set to the desired occurrence number before the SORT statement
is executed.
- If the KEY phrase is not specified, the sequence is determined by the KEY phrase in the Data Description entry of the table
referenced by data-name-2.
- If the KEY phrase is specified, it overrides any KEY phrase specified in the Data Description entry of the table referenced
by data-name-2.
- If the key phrase is specified but data-name-1 is omitted, the data item referenced by data-name-2 is the KEY data item.
- The sorted table elements of the table referenced by data-name-2 are placed in the table referenced by data-name-2.
- SORT procedures should not be invoked by multiple threads simultaneously.
Examples
Examples of using SORT TABLE are provided in the topic
SORT Table Entries.