Sorting with the SORT verb
| The SD file | |
| The SORT verb | |
| Input and output procedures |
The SD file
SORT works on an SD (sort description) file in DATA DIVISION. Its record layout matches the records being sorted, and the sort keys are named in the SORT verb itself:
DATA DIVISION.
FILE SECTION.
SD SORT-FILE.
01 SORT-RECORD.
05 SORT-DEPT PIC X(10).
05 SORT-PAY PIC 9(7)V99.
The SORT verb
SORT names the work file, the keys in ascending or descending order, and where records come from and go. ON ASCENDING KEY sorts by department, then pay:
SORT SORT-FILE
ON ASCENDING KEY SORT-DEPT
ON DESCENDING KEY SORT-PAY
USING UNSORTED-FILE
GIVING SORTED-FILE.
Input and output procedures
INPUT PROCEDURE filters records before the sort, and OUTPUT PROCEDURE processes them after, one at a time with RETURN. Both are optional: USING and GIVING alone cover the common case.
Next: Command-line arguments
Article author: Arthur Isaev