In COBOL74 or COBOL85, the SORT intrinsic is invoked by the SORT statement. The sort work file, input procedure or file, output procedure or file, sort keys, and sorting criteria are specified in the SORT statement. The sort work file must be described in a sort-merge file-description entry in the DATA DIVISION and in a SELECT clause in the FILE-CONTROL paragraph. The SELECT clause determines the sort mode to be used.
Memory size, disk size, and restart information are optional parameters. For a complete description of the COBOL74 or COBOL85 SORT statement, refer to the COBOL ANSI-74 Programming Reference Manual, Volume 1: Basic Implementation or the COBOL ANSI-85 Programming Reference Manual, Volume 1: Basic Implementation.
SORT Input/Output (I/O) Procedure Logic Flow
The following logic chart shows the interaction of the SORT utility with a COBOL procedural sort operation:
-
Sort 1. Begin SORT initialization phase.
-
Sort 2. Open sort work files.
-
Sort 3. Go to beginning of your INPUT PROCEDURE.
-
IP1. = Open input file.
-
IP2. = Read input file record (at end, go to IP6).
-
IP3. = If record is to be used, place in record area of sort file; otherwise, go to IP2.
-
IP4. = Release sort file record (transfer to Sort 4).
-
-
Sort 4. Place the released record in sorting process.
-
Sort 5. Execute internal sorting, creating strings on sort work files.
-
Sort 6. Return to INPUT PROCEDURE at IP5.
-
IP5. = Execute using logic; then go to IP2.
-
IP6. = Execute AT END logic, including close of input file (transfer to Sort 7).
-
-
Sort 7. Complete SORT stringing of all input records.
-
Sort 8. Begin merge phase of SORT.
-
Sort 9. Merge all strings on sort work files until one string remains.
-
Sort 10. Go to beginning of OUTPUT PROCEDURE.
-
OP1. = Open output file (transfer to Sort 11).
-
-
Sort 11. Execute final internal merging operation.
-
Sort 12. = Pass merged record to OUTPUT PROCEDURE at OP2.
-
OP2. = Return sort file record to user record area (at end, go to OP4).
-
OP3. = Execute user logic (transfer to Sort 11).
-
OP4. = Execute AT END logic, including close of output file (transfer to sort 13).
-
-
Sort 13. = Close all sort work files.
-
Sort 14. Exit from SORT.
COBOL74 and COBOL85 SORT Example
The following is an example of a disk sort that you can use for either COBOL74 or COBOL85, including the actual input and output files:
IDENTIFICATION DIVISION. ENVIRONMENT DIVISION. CONFIGURATION SECTION. SOURCE-COMPUTER. A-SERIES. OBJECT-COMPUTER. A-SERIES. DISK SIZE 20000 WORDS MEMORY SIZE 3000 WORDS. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT NEWTRANS ASSIGN TO READER. SELECT CREDITFILE ASSIGN TO DISK. SELECT DEBITFILE ASSIGN TO DISK. SELECT SORTFILE ASSIGN TO SORT DISK. DATA DIVISION. FILE SECTION. FD NEWTRANS VALUE OF TITLE "TRANSACTIONS". 01 NEWTR PIC X(80). FD CREDITFILE VALUE OF TITLE "NEW"/"CREDITS" BLOCK CONTAINS 15 RECORDS. 01 CR-REC PIC X(80). FD DEBITFILE VALUE OF TITLE "NEW"/"DEBITS" BLOCK CONTAINS 15 RECORDS. 01 DR-REC PIC X(80). SD SORTFILE. 01 SRT. 03 CODE-KEY PIC 99. 03 ACCOUNT-KEY PIC 9(10). 03 DATE-KEY PIC 9(6). 03 FILLER PIC X(62). PROCEDURE DIVISION. SORTIT SECTION. SRTRN. SORT SORTFILE ON DESCENDING KEY CODE-KEY ASCENDING KEY ACCOUNT-KEY DATE-KEY USING NEWTRANS OUTPUT PROCEDURE RECORDS-OUT. ENDIT. STOP RUN. RECORDS-OUT SECTION. CREDITS-OUT. OPEN OUTPUT CREDITFILE DEBITFILE. LOOP-CR. RETURN SORTFILE AT END GO TO XIT. IF CODE-KEY > 49 WRITE CR-REC FROM SRT INVALID KEY GO TO IVK. GO TO LOOP-CR. LOOP-DR. WRITE DR-REC FROM SRT INVALID KEY GO TO IVK. RETURN SORTFILE AT END GO TO XIT GO TO LOOP-DR. XIT. CLOSE CREDITFILE LOCK DEBITFILE LOCK. GO TO ENDIT. IVK. DISPLAY "ERROR TERMINATION". DISPLAY CODE-KEY ACCOUNT-KEY. ENDIT. EXIT.
Input File to be Sorted:
121000000233040770 121000000233040970 121000000233041270 031200000042041270 031200000042041370 031200000042041670 031200000042040970 551000000012050170 551000000012052370 551000000012051470 551000000012050570 471000000012050570 471000000012052270 720900000243060270 720900000243061970 720900000243062170 710900000243062170 710900000243062670 124000000035062670 900000000017070370
Sorted Output Files:
NEW/CREDITS output file
900000000017070370 720900000243060270 720900000243061970 720900000243062170 710900000243062170 710900000243062670 551000000012050170 551000000012050570 551000000012051470 551000000012052370
NEW/DEBITS output file
471000000012050570 471000000012052270 121000000233040770 121000000233040970 121000000233041270 124000000035062670 031200000042040970 031200000042041270 031200000042041370 031200000042041670