COBOL does not let sorting arrays but there is an IBM COBOL sorting method for sequential files. This method is stable, fast (O(n log n)), multi-phase merge-sort and quicksort hybrid optimized for large datasets. See COBOL DATA STRUCTURES for information on writing array to file and reading file to array.
Because sorting is I/O heavy, best practice is sorting the original file into a sorted version, then processing the new file via many SEARCH ALLs. The filename of the new file should show the original filename plus something mentioning the sorted status (e.g. SORTED-CUSTOMER.DAT, CUSTOMER.SRT), and it also will be sequential.
In the example the UNSORTED-FILE contains the source, the SORTED-FILE will be the sorted version, the SORT-WORK-FILE is temporal in the buffer; the direction is defined by ASCENDING or DESCENDING, after the KEY clause the record field is defined. The temporal file is handled automatically by the SORT.
The physical PD, PDS, VSAM ESDS, VSAM RRDS and GDG files can be read as SEQUENTIAL.
The physical VSAM KSDS can be read as INDEXED, therefore pre- and post-sorting steps are needed:
read records from INDEXED source file (READ NEXT)
write records into temporal SEQUENTIAL file
SORT
read records from sorted SEQUENTIAL file
write records into targeted INDEXED file
EXAMPLE FOR FILE SORTING, SEQUENTIAL:
EXAMPLE FOR FILE SORTING, INDEXED: