The SORT statement takes records from one or more files, sorts them according to the provided keys and makes the sorted records available either through an output procedure or in an output file. The SORT statement can code anywhere in the procedure division except in the declarative portion.

Syntax -

SORT Syntax

file-name-1 -

Specifies the file name in the SD entry.

ASCENDING KEY and DESCENDING KEY phrase -

ASCENDING KEY and DESCENDING KEY phrase -

Specifies the records are to be processed in ascending or descending sequence depending on the phrase specified.

data-name-1 -

Specifies a KEY data item used for sorting. The data-names following the word KEY are listed from left to right in order of decreasing significance. The leftmost data-name is the major key, the next data-name is the next most significant key, and so forth.

Rules: -

  • A specific KEY data item must be physically located in the same position and have the same data format in each input file.
  • If file-name-1 has more than one record description, the KEY data items need be specifies 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 minimum records 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.
  • KEY data items cannot be -
    • Variably located.
    • Group items that contain variable-occurrence data items.
    • Date fields
  • KEY data items can be qualified.
  • KEY data items can be any of the following data categories -
    • Alphabetic, alphanumeric, alphanumeric-edited.
    • Numeric (except numeric with usage NATIONAL).
    • Numeric-edited (with usage DISPLAY).
    • Internal floating-point or display floating-point.

If file-name-3 references an indexed file, the first specification of data-name-1 must be associated with an ASCENDING phrase and the data item referenced by that data-name-1 must occupy the same character positions in this record as the data item associated with the major record key for that file. The direction of the sorting operation depends on 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 arranged depends on the collating sequence.
  • If the KEY is a display floating-point item, the compiler treats the data item as character data of the same size as the key. 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 will be in numeric order.
  • When the COLLATING SEQUENCE phrase is not specified, the key comparisons are performed according to the rules for comparison of operands in a relation condition.
  • When the COLLATING SEQUENCE phrase is specified, the indicated collating sequence is used for key data items of alphabetic, alphanumeric, alphanumeric-edited, external floating-point and numeric-edited categories.

DUPLICATES phrase -

If all the keys associated with one record are equal to the corresponding keys in one or more other records, then considered that file has duplicate records. DUPLICATES phrase used to specify the order when the duplicate records existed in the file.

If the DUPLICATES phrase is specified and duplicates existed, the order of associated input files as specified in the SORT statement. If the DUPLICATES phrase is not specified, the order of these records is undefined.

COLLATING SEQUENCE phrase -

Specifies the collating sequence to be used in alphanumeric comparisons for the KEY data items in this sort operation. When the COLLATING SEQUENCE phrase is specified, the indicated collating sequence is used for key data items comparisons of alphabetic, alphanumeric, alphanumeric-edited, external floating-point and numeric-edited categories.

When the COLLATING SEQUENCE phrase is not specified, the key comparisons are performed according to the rules for comparison of operands. The COLLATING SEQUENCE phrase has no effect for keys that are not alphabetic or alphanumeric.

alphabet-name-1 -

Must be specified in the ALPHABET clause of the SPECIAL-NAMES paragraph. When the COLLATING SEQUENCE phrase is omitted, the PROGRAM COLLATING SEQUENCE clause in the OBJECT-COMPUTER paragraph used.

When both the COLLATING SEQUENCE phrase and the PROGRAM COLLATING SEQUENCE clause are omitted, the collating sequence indicated by the COLLSEQ compiler option is used.

USING phrase -

file-name-2, file-name-3, ...

Specifies the input files. During the SORT operation, all the records from file-name-2, file-name-3, ... are transferred to file-name-1. When SORT statement is executed, these files should not be opened. The input files are automatically opened, read and closed.

All input files must specify sequential or dynamic access mode and be defined in FD entries in the data division. If file-name-1 contains variable-length records, the size of the records contained in the input files (file-name-2, file-name-3, ...) must be neither less than the smallest record nor greater than the largest record of the file-name-1. If file-name-1 contains fixed-length records, the size of the records contained in the input files must not be greater than the largest record of file-name-1.

INPUT PROCEDURE phrase -

Specifies the name of a procedure used to select or modify input records before the sorting operation begins.

procedure-name-1 -

Specifies the first (or only) section or paragraph in the input procedure.

procedure-name-2 -

Specifies the last section or paragraph of the input procedure. The input procedure can consist of any procedure required to select, modify or copy the records that are made available one at a time by the RELEASE statement to the file referenced by file-name-1.

The range of the input procedure must not cause the execution of any MERGE, RETURN or SORT statement. If an input procedure is specified, control is passed to the input procedure before the file-name-1 is sequenced by the SORT statement.

The compiler inserts a return mechanism at the end of the last statement in the input procedure. When control passes the last statement in the input procedure, the records released to the file-name-1 are sorted.

GIVING phrase -

file-name-3, ...

Specifies the output files.

When the GIVING phrase is specified, all the sorted records in file-name-1 are automatically transferred to the output files (file-name-4, ...). All output files must specify sequential or dynamic access mode and be defined in FD entries in the data division.

When SORT statement is executed, the output files (file-name-4, ...) should not be opened. The output files are automatically opened, read and closed.

If the output files (file-name-4, ...) contain variable-length records, the size of the records contained in file-name-1 must be neither less than the smallest record nor greater than the largest record of output files. If the output files contain fixed-length records, the size of the records contained in file-name-1 must not be greater than the largest record described for the output files.

OUTPUT PROCEDURE phrase -

Specifies the name of a procedure used to select or modify output records from the sort operation.

procedure-name-3 -

Specifies the first (or only) section or paragraph in the OUTPUT PROCEDURE.

procedure-name-4 -

Identifies the last section or paragraph in the OUTPUT PROCEDURE.

The OUTPUT PROCEDURE can consist of any statements needed to select, modify or copy the records that are made available by the RETURN statement in sorted order from the file referenced by file-name-1.