The SORT TABLE Statement

The SORT TABLE feature can be used to sort the elements of a table.

General Format


*

Syntax Rules

  1. Data-name-1 is a KEY data-name and is subject to the following rules:
    1. KEY data-names can be qualified.
    2. The data items identified by KEY data-names must not be variable length items.
    3. KEY data items can be floating-point items.
    4. 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.
    5. If the KEY data item is internal floating-point, the records are sorted in numeric order of key values.
  2. Data-name-2 can be qualified and must have an OCCURS clause in the data description entry.
  3. 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.
  4. 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.
  5. The KEY phrase can be omitted only if the description of the table referenced by data-name-2 contains a KEY phrase.
  6. 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

  1. The words ASCENDING and DESCENDING are transitive across all occurrences of data-name-1 until another word ASCENDING or DESCENDING is encountered.
  2. 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.
  3. 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:
    1. 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.
    2. The order in which these records are released by an input procedure, when an input procedure is specified.
    3. The relative order of the contents of these table elements before sorting takes place.
  4. 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.
  5. 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:
    1. First, the collating sequence established by the COLLATING SEQUENCE phrase, if specified, in the SORT statement.
    2. Second, the collating sequence established as the program collating sequence.
  6. 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.
  7. 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.
    1. 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.
    2. 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.
    3. 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.
  8. The number of occurrences of table elements referenced by data-name-2 is determined by the rules in the OCCURS clause.
  9. 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.
  10. 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.
  11. 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.
  12. 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.
  13. The sorted table elements of the table referenced by data-name-2 are placed in the table referenced by data-name-2.
  14. SORT procedures should not be invoked by multiple threads simultaneously.

Examples

Examples of using SORT TABLE are provided in the topic SORT Table Entries.