An external sort utility is a separate program or JCL step that sorts (or merges) data outside your COBOL program. On the mainframe, DFSORT (or SYNCSORT) is commonly used: the job runs the sort step, then your COBOL program reads or writes the sorted file. This page explains what an external sort is, when to use it instead of the COBOL SORT statement, and how COBOL programs work with externally sorted data.
Imagine you have a stack of cards to sort. In-program sort is like sorting them yourself at your desk: you use COBOL (RELEASE, SORT, RETURN) to do it inside your program. External sort is like giving the stack to a separate machine that sorts them, then giving you back the sorted stack. Your program only sees the result: a file that is already in order. The "machine" is the sort utility (e.g. DFSORT) run as its own step in the job.
An external sort utility runs as a separate step in a batch job. It reads from an input file (or multiple files for merge), sorts (or merges) the records, and writes to an output file. The COBOL program does not execute the sort; it runs in a different step and simply uses the sorted file. On z/OS, the usual utility is DFSORT (or a compatible product like SYNCSORT). You invoke it via JCL and pass control statements (SORT FIELDS=, INCLUDE/OMIT, SUM, OUTREC, etc.) to define keys and optional transformations.
A typical flow is: (1) An earlier step (or a prior job) creates an unsorted file. (2) The sort step runs: SORTIN is the input, SORTOUT is the output, and the utility sorts by the specified keys. (3) Your COBOL program runs in the next step and opens SORTOUT as a normal sequential file. It reads records in sorted order; no SORT statement is needed. If your program produces a file that must be sorted for a downstream job, your step writes a file that becomes SORTIN for a later sort step.
123456789101112131415161718192021*> This program reads a file that was sorted by a previous JCL step. *> It does not perform the sort; it just processes records in order. FILE-CONTROL. SELECT SORTED-FILE ASSIGN TO SORTOUT. DATA DIVISION. FD SORTED-FILE. 01 SORTED-RECORD. 05 KEY-FIELD PIC X(10). 05 DATA-FIELD PIC X(70). PROCEDURE DIVISION. MAIN. OPEN INPUT SORTED-FILE PERFORM UNTIL no-more READ SORTED-FILE AT END SET no-more TO TRUE NOT AT END PERFORM PROCESS-RECORD END-READ END-PERFORM CLOSE SORTED-FILE STOP RUN.
In-program sorting uses the COBOL SORT statement with a sort work file (SD), RELEASE in an INPUT PROCEDURE, and RETURN in an OUTPUT PROCEDURE. You control filtering and transformation in COBOL. External sort uses the system utility: you define keys and options in control cards; the utility does the sort. The table below summarizes the main differences.
| Aspect | In-program (COBOL SORT) | External (e.g. DFSORT) |
|---|---|---|
| Where it runs | Inside the COBOL program (SORT statement) | Separate step in the job (e.g. DFSORT) |
| Input/output | USING/GIVING or INPUT/OUTPUT PROCEDURE | SORTIN/SORTOUT (and work files) in JCL |
| Filtering/transform | INPUT/OUTPUT PROCEDURE in COBOL | INCLUDE/OMIT, INREC, OUTREC in control cards |
| Typical use | Moderate size; need COBOL logic during sort | Very large sorts; use utility features |
1. How does a COBOL program typically use an external sort?
2. When is external sort often preferred over in-program SORT?