SORT Statement

The SORT statement accepts records from one or more files, sorts them according to the specified key(s), and makes the sorted records available either through an OUTPUT PROCEDURE or in an output file. A SORT statement may appear anywhere in the Procedure Division except in a Declarative Section. The maximum number of USING or GIVING files is 32.

SORT Statement - Format

Read syntax diagramSkip visual syntax diagramSORTfile-name-1ONASCENDINGDESCENDINGKEYdata-name-1WITHDUPLICATESIN ORDERCOLLATINGSEQUENCEISalphabet-nameUSINGfile-name-2input procedure phraseGIVINGfile-name-3output procedure phrase
input procedure phrase
Read syntax diagramSkip visual syntax diagramINPUT PROCEDUREISprocedure-name-1THROUGHTHRUprocedure-name-2
output procedure phrase
Read syntax diagramSkip visual syntax diagramOUTPUT PROCEDUREISprocedure-name-3THROUGHTHRUprocedure-name-4
file-name-1
The name given in the SD entry that describes the records being sorted.

Null-capable fields are supported, but null values are only supported for DATABASE files that have ALWNULL specified on their ASSIGN clause. If ALWNULL is not specified, the SORT operation will fail, and file status of 90 will be returned if a field contains a null value.