TExcelTAD
Home › COBOL › COBOL SORT & MERGE

COBOL SORT & MERGE

Sorting is central to batch processing: transactions are sorted by account before they are applied to a master file, and reports are sorted by region or date. COBOL has a built-in SORT verb that uses a temporary sort file, described with an SD (sort description) entry instead of an FD.

cobol
           SORT sort-file
               ON ASCENDING KEY key-1 [ON DESCENDING KEY key-2 ...]
               USING input-file | INPUT PROCEDURE IS paragraph
               GIVING output-file | OUTPUT PROCEDURE IS paragraph

SORT USING / GIVING

The simplest form sorts one file into another. SORT opens and closes both files itself:

cobol · try it yourself
       IDENTIFICATION DIVISION.
       PROGRAM-ID. SORT-FILES.
       ENVIRONMENT DIVISION.
       INPUT-OUTPUT SECTION.
       FILE-CONTROL.
           SELECT UNSORTED ASSIGN TO "unsorted.dat"
               ORGANIZATION IS LINE SEQUENTIAL.
           SELECT SORTED   ASSIGN TO "sorted.dat"
               ORGANIZATION IS LINE SEQUENTIAL.
           SELECT SORT-WORK ASSIGN TO "sortwork.tmp".
       DATA DIVISION.
       FILE SECTION.
       FD  UNSORTED.
       01  UNSORTED-REC           PIC X(16).
       FD  SORTED.
       01  SORTED-REC             PIC X(16).
       SD  SORT-WORK.
       01  SORT-REC.
           05  SR-REGION          PIC X(6).
           05  SR-NAME            PIC X(6).
           05  SR-SALES           PIC 9(4).
       WORKING-STORAGE SECTION.
       01  WS-EOF                 PIC X       VALUE "N".
       PROCEDURE DIVISION.
           OPEN OUTPUT UNSORTED
           WRITE UNSORTED-REC FROM "WEST  Kim   0450"
           WRITE UNSORTED-REC FROM "EAST  Lea   0700"
           WRITE UNSORTED-REC FROM "WEST  Raj   0910"
           WRITE UNSORTED-REC FROM "EAST  Ana   0300"
           WRITE UNSORTED-REC FROM "NORTH Tom   0525"
           CLOSE UNSORTED

           SORT SORT-WORK
               ON ASCENDING KEY SR-REGION
               ON DESCENDING KEY SR-SALES
               USING UNSORTED
               GIVING SORTED

           OPEN INPUT SORTED
           PERFORM UNTIL WS-EOF = "Y"
               READ SORTED
                   AT END MOVE "Y" TO WS-EOF
                   NOT AT END DISPLAY SORTED-REC
               END-READ
           END-PERFORM
           CLOSE SORTED
           STOP RUN.
EAST  Lea   0700
EAST  Ana   0300
NORTH Tom   0525
WEST  Raj   0910
WEST  Kim   0450

The records are ordered by region (A–Z) and, within each region, by sales from highest to lowest. The keys are fields of the SD record, listed from most to least significant.

INPUT PROCEDURE and OUTPUT PROCEDURE

Instead of a file, the sort can be fed by an input procedure that hands records to the sort with RELEASE, and drained by an output procedure that fetches sorted records with RETURN. This lets you filter, compute or report while sorting, with no intermediate files of your own:

cobol · try it yourself
       IDENTIFICATION DIVISION.
       PROGRAM-ID. SORT-PROCEDURES.
       ENVIRONMENT DIVISION.
       INPUT-OUTPUT SECTION.
       FILE-CONTROL.
           SELECT SORT-WORK ASSIGN TO "sortwork.tmp".
       DATA DIVISION.
       FILE SECTION.
       SD  SORT-WORK.
       01  SORT-REC.
           05  SR-NAME            PIC X(8).
           05  SR-SCORE           PIC 999.
       WORKING-STORAGE SECTION.
       01  WS-DATA.
           05  FILLER             PIC X(11)   VALUE "Mia     072".
           05  FILLER             PIC X(11)   VALUE "Noah    095".
           05  FILLER             PIC X(11)   VALUE "Zoe     048".
           05  FILLER             PIC X(11)   VALUE "Liam    088".
           05  FILLER             PIC X(11)   VALUE "Ava     095".
       01  WS-TABLE REDEFINES WS-DATA.
           05  WS-ENTRY           PIC X(11)   OCCURS 5 TIMES.
       01  WS-I                   PIC 9.
       01  WS-RANK                PIC 9       VALUE 0.
       01  WS-DONE                PIC X       VALUE "N".
       PROCEDURE DIVISION.
           SORT SORT-WORK
               ON DESCENDING KEY SR-SCORE
               ON ASCENDING KEY SR-NAME
               INPUT PROCEDURE IS LOAD-PASSES
               OUTPUT PROCEDURE IS PRINT-RANKING
           STOP RUN.

       LOAD-PASSES.
           PERFORM VARYING WS-I FROM 1 BY 1 UNTIL WS-I > 5
               MOVE WS-ENTRY (WS-I) TO SORT-REC
               IF SR-SCORE >= 50
                   RELEASE SORT-REC
               END-IF
           END-PERFORM.

       PRINT-RANKING.
           PERFORM UNTIL WS-DONE = "Y"
               RETURN SORT-WORK
                   AT END MOVE "Y" TO WS-DONE
                   NOT AT END
                       ADD 1 TO WS-RANK
                       DISPLAY WS-RANK ". " SR-NAME SR-SCORE
               END-RETURN
           END-PERFORM.
1. Ava     095
2. Noah    095
3. Liam    088
4. Mia     072

Zoe's score of 48 is filtered out in the input procedure. Noah and Ava tie on 95, so the second key puts them in alphabetical order.

Sorting a table in memory

GnuCOBOL (following COBOL 2002) can also sort a table directly, with no sort file. The table needs a KEY clause or the keys are given in the statement:

cobol · try it yourself
       IDENTIFICATION DIVISION.
       PROGRAM-ID. TABLE-SORT.
       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01  WS-PRICES-DATA         PIC X(20)
           VALUE "04999012500999900350".
       01  WS-PRICES REDEFINES WS-PRICES-DATA.
           05  WS-PRICE           PIC 999V99  OCCURS 4 TIMES.
       01  WS-I                   PIC 9.
       PROCEDURE DIVISION.
           SORT WS-PRICE ON ASCENDING KEY WS-PRICE
           PERFORM VARYING WS-I FROM 1 BY 1 UNTIL WS-I > 4
               DISPLAY WS-PRICE (WS-I)
           END-PERFORM
           STOP RUN.
003.50
012.50
049.99
099.99

MERGE

MERGE combines two or more files that are already sorted on the same keys into one sorted output, for example yesterday's and today's transaction files. It works like SORT … USING … GIVING, with several USING files:

cobol
           MERGE MERGE-WORK
               ON ASCENDING KEY MW-ACCOUNT
               USING TRANSACTIONS-MON TRANSACTIONS-TUE
               GIVING TRANSACTIONS-WEEK

Tip: On mainframes, much sorting is done outside COBOL by the system sort utility (DFSORT or Syncsort), run as its own JCL step. COBOL's SORT verb calls the same utility under the covers.

Test yourself

Which entry describes a sort work file?

  1. FD
  2. SD
  3. RD
  4. WS

In an INPUT PROCEDURE, which verb passes a record to the sort?

  1. WRITE
  2. RELEASE
  3. RETURN
  4. PASS

What does MERGE require of its input files?

  1. Nothing
  2. They must already be sorted on the merge keys
  3. They must be indexed
  4. They must be empty