ASCENDING/DESCENDING KEY Phrase

This phrase specifies that records are to be processed in ascending or descending sequence (depending on the phrase specified), based on the specified sort keys.
data-name-1
Specifies a KEY data item on which the sort will be based. Each such data-name must identify a data item in a record associated with file-name-1. The following rules apply:
  • A specific KEY data item must be physically located in the same position and have the same data format in each input file. However, it need not have the same data-name.
  • If file-name-1 has more than one record description, then the KEY data items need be described in only one of the record descriptions.
  • If file-name-1 contains variable length records, all of the KEY data-items must be contained within the first n character positions of the record, where n equals the maximum record size specified for file-name-1.
  • KEY data items must not contain an OCCURS clause or be subordinate to an item that contains an OCCURS clause.
  • The total length of the KEY data item must not exceed 2 000 bytes.
  • KEY data items can be qualified, but they cannot be subscripted or indexed.
  • KEY data items cannot be variably-located.
  • Variable length fields can not be used in a SORT key as a variable length field. Variable length fields are converted into group items by ILE COBOL. Since variable length fields are converted into group items, they are compared as alphanumeric data items when used in a SORT key.

SORT lists the KEY data items from left to right in order of decreasing significance, no matter how they are divided into KEY phrases. The leftmost data-name is the major key, the next data-name is the next most significant key, and so forth.

The direction of the sorting operation depends on the specification of the ASCENDING or DESCENDING keywords as follows:
  • When ASCENDING is specified, the sequence is from the lowest key value to the highest key value.
  • When DESCENDING is specified, the sequence is from the highest key value to the lowest.
  • If the KEY data item is alphabetic, alphanumeric, alphanumeric-edited, or numeric-edited, the sequence of key values depends on the collating sequence used (see COLLATING SEQUENCE Phrase).
  • If the KEY data item is DBCS, DBCS-edited, or national, the sequence of key values is based on a binary collating sequence of the hexadecimal values of the DBCS or national characters. The COLLATING SEQUENCE phrase is ignored.
IBM Extension
IBM Extension
  • KEY data items can be floating-point or date-time items.
  • KEY data items can be reference modified, but they cannot be subscripted or indexed.
  • 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 sequence of key values is in numeric order.
  • If the KEY is a date-time item, only some formats will be sorted as date or time items. ILE COBOL supports many more date-time formats than IBM® i DDS. In general, ILE COBOL date-time formats that match an IBM i DDS format are sorted as a date or time item; all other formats are treated as alphanumeric items, and are sorted based on their hexadecimal value.
End of IBM Extension
End of IBM Extension
  • The key comparisons are performed according to the rules for comparison of operands in a relation condition (see "Relation Condition" under Conditional Expressions).