COBOL Manual for TNS/E Programs (H06.08+, J06.03+)
cause the execution of any statement that manipulates any of the input files or accesses
the record area associated with any input file.
◦ INPUT PROCEDURE phrase
If the INPUT PROCEDURE phrase appears, the input phase executes an implicit PERFORM
procedure-group statement (where procedure-group is procedure name, optionally
followed by THROUGH or THRU and another procedure name). The sort input procedure
thereby specified is responsible for releasing the records to be sorted by one or more
executions of RELEASE statements. The sort input procedure must not open a file specified
by outfile. Control automatically returns to the sort operation after completing the
execution of the implicit PERFORM statement.
The range of the implicit procedure must not cause the execution of a MERGE, RETURN,
or SORT statement or a RELEASE statement that references any sort or merge file other
than the one identified by sd-name.
• Sort Phase of Execution
The sort phase reorders the records within the sort file according to their sort key values. The
records of sd-name are first sorted in accordance with their values for the most significant
(first listed) sort key. When two or more records have equal values for the current sort key,
that group of records is then sorted in accordance with the record values for the next most
significant key, and so on. When two or more records contain equal values for all sort keys,
their final order within the sort file depends on the DUPLICATES phrase. If DUPLICATES is
specified, their order will be the order in which they were released to the sort file. If
DUPLICATES is not specified, their order is arbitrary.
The key s are listed from left to right within the SORT statement in order of decreasing
significance, without regard to how the list of keys is divided into ASCENDING KEY or
DESCENDING KEY phrases; therefore, the data item specified by the first key is the most
significant sort key, and the data item specified by the last key is the least significant sort key.
The ordering implied by a sort key depends upon its KEY phrase:
◦ When the sort key is specified in an ASCENDING KEY phrase, the sorted sequence is
from the record having the lowest value in the sort key data item to the record having the
highest value in the sort key data item.
◦ When the sort key is specified in a DESCENDING KEY phrase, the sorted sequence is
from the record having the highest value in the sort key data item to the record having
the lowest value in the sort key data item.
◦ In both cases, the determination of which value is higher is made according to the rules
for comparison of operands in a relation condition.
The collating sequence that applies to the comparison of nonnumeric sort key data items is
the one specified by the alphabet-name in the COLLATING SEQUENCE phrase. When this
phrase is not specified, the program collating sequence applies.
• Output Phase of Execution
GIVING phrase
If the GIVING phrase appears, the output phrase transfers all records in the sd-name
file to each output file outfile. Each output file must be in the closed state, but not in
◦
the locked state, when the output phase begins executing. First, the output phrase implicitly
opens each output file in the output mode. The output phase then executes implicit return
operations to retrieve the records from the sort file and implicit WRITE statements write
operations to release them to each of the output files. Finally, the output phase implicitly
closes each of the output files.
SORT 447










