COBOLadvanced

Sorting Internal Tables

Know the techniques for sorting COBOL tables in memory when data must be ordered before further processing.

While DFSORT and SORT utilities handle sorting entire datasets at the JCL level, COBOL also needs to sort data already loaded into memory as a table — for instance, ranking a set of records read into an OCCURS table before producing a summary report. This is a more advanced topic because native COBOL (pre-2002 standard) has no built-in table-sort verb, so the classic approach is either a hand-rolled algorithm or the SORT verb with an internal input/output procedure.

Sorting an in-memory COBOL table by hand is like re-alphabetizing a small stack of index cards on your desk yourself, versus sending a whole filing cabinet's worth of paperwork to an outside sorting service — fine for a dozen cards, a bad idea for ten thousand.

Key Concepts

1
Modern COBOL compilers (including IBM Enterprise COBOL) support a SORT statement that can sort an internal table directly when combined with an INPUT PROCEDURE and OUTPUT PROCEDURE, or in some dialects a direct table sort is available. Before this was standard, most shops implemented a bubble sort or shell sort by hand directly against the OCCURS table using nested PERFORM VARYING loops — inefficient for large tables but adequate for the modestly sized tables (dozens to low hundreds of entries) typical in COBOL memory processing.
2
The interview angle here usually isn't 'write a full sort algorithm from scratch' but rather demonstrating you understand *why* in-memory table sorting is a different problem from dataset sorting: it happens after data is already loaded, it competes for the program's own working storage, and for large volumes it's almost always better architecture to let DFSORT sort the data before it ever reaches the COBOL program, reserving in-memory sorts for genuinely small working sets.
3
A sharp answer acknowledges the tradeoff explicitly: use JCL-level SORT/DFSORT for large datasets before COBOL even opens the file, and reserve in-program table sorts for small, already-in-memory collections where invoking an external utility would be architectural overkill.