Chapter 14. Sorting and Merging


To familiarize you with

  1. How files may be sorted within a COBOL program.

  2. How to process a file during a SORT procedure before it is actually sorted.

  3. How to process a file during a SORT procedure after it is sorted but before it is created as output.

  4. How to use the MERGE verb for merging files.


Format of the SORT Statement

Records in files frequently must be sorted into specific sequences for updating, answering inquiries, or generating reports. A master payroll file, for example, might be updated in Social Security number sequence while paychecks produced from the file may be needed in alphabetic order. Sorting is a common procedure used for arranging records into a specific order so that sequential batch processing can be performed.

There are two techniques used for sorting files processed by COBOL programs. One is to use either a utility or a database management system's sort program. These sort programs are completely separate from, or external to, the COBOL program and would be executed first if records needed to be in a sequence other than the sequence in which they appear in the file. For these types of sort programs, you would simply indicate which key fields to sort on.

As an alternative, COBOL has a SORT verb, which enables you to sort a file as part of a COBOL program. Often, a COBOL program will SORT a file prior to processing it.

A simplified format for the SORT statement in COBOL is as follows:

Simplified Format ...

Get COBOL for the 21st Century now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.