The SORT statement handles both files and tables.
1 – 1format_file
The SORT statement creates a sort file by executing input procedures or transferring records from an input file. It sorts the records in the sort file using one or more keys that you specify. Finally, it returns each record from the sort file, in sorted order, to output procedures or an output file. Format - SORT sortfile { ON { ASCENDING } KEY { sortkey } ... } ... { { DESCENDING } } [ WITH DUPLICATES IN ORDER ] [ COLLATING SEQUENCE IS alpha ] { INPUT PROCEDURE IS first-proc [ { THRU } end-proc ] } { [ { THROUGH } ] } { USING { infile } ... } { OUTPUT PROCEDURE IS first-proc [ { THRU } end-proc ] } { [ { THROUGH } ] } { GIVING { outfile } ... }
1.1 – sortfile
is a file-name described in a sort-merge file description (SD) entry in the Data Division.
1.2 – sortkey
is the data-name of a data item in a record associated with sortfile.
1.3 – alpha
is an alphabet-name defined in the SPECIAL-NAMES paragraph of the Environment Division.
1.4 – first-proc
is the section-name or paragraph-name of the first (or only) section or paragraph of the INPUT or OUTPUT procedure range.
1.5 – infile
is the file-name of the input file. It must be described in a file description (FD) entry in the Data Division.
1.6 – end-proc
is the section-name or paragraph-name of the last section or paragraph of the INPUT or OUTPUT procedure range.
1.7 – outfile
is the file-name of the output file. It must be described in a file description (FD) entry in the Data Division.
2 – 2format_table
The SORT statement orders a table. It sorts the table elements based on the keys as specified in the OCCURS for the table. The table keys as specified in the OCCURS can be overridden with keys as specified in the SORT statement. If no key is specified, the table elements are the SORT keys. Format - SORT table-name [ ON { ASCENDING } KEY { sortkey } ... ] ... [ { DESCENDING } ] [ WITH DUPLICATES IN ORDER ] [ COLLATING SEQUENCE IS alpha ].
2.1 – table-name
is a table described with OCCURS in the Data Division.
2.2 – sortkey
is the data-name of a data item in the table-name table.
2.3 – alpha
is an alphabet-name defined in the SPECIAL-NAMES paragraph of the Environment Division.