COBOL Sort Verb
The SORT Verb
DATA DIVISION.
File Section.
FDSourceFile.
01Source-Rec.
FDTargetFile.
01Target-Rec.
SDSortFile.
01Sort-Rec.
05SA
05SB
05SC
:
PROCEDURE DIVISION.
:
SORT SortFile
ON ASCENDING KEY SB[Up to 12 keys]
ON DESCENDING KEY SA
{USING SourceFileor INPUT PROCEDURE IS 000-InputSecName }
{GIVING TargetFileorOUTPUT PROCEDURE IS 000-OutputSecName }
*> The SORT verb opens the SortFile for output for use by the USING
*> clause or by the INPUT PROCEDURE.
000-InputSecName SECTION.
100-Init-Para.
OPEN INPUT SourceFile. *> The programmer opens the Source file,
*> but NEVER the Sort file.
200-Process-Loop-Para.
PERFORM UNTIL There-Are-No-More-Records
READ SourceFile [INTO A-Working-Storage-Record]
AT END
MOVE "NO" to Are-There-More-Records
CLOSE SourceFile
NOT AT END
*> Process the Source-Rec, or a Source-Rec might be
*> skipped or discarded.
MOVE the processed record to Sort-Rec
RELEASE Sort-Rec [FROM A-Working-Storage-Record]
END-READ
END-PERFORM.
MOVE "YES" to Are-There-More-Records
*> The SORT verb closes the SortFile, and its records are ready to be
*> sorted.
*> The sort of all the records released to the SortFile is carried
*> out. Then the SORT opens the SortFile for input to be used by the
*> GIVING clause or by the OUTPUT PROCEDURE.
000-OutputSecName SECTION.
100-Init-Para.
OPEN OUTPUT TargetFile. *> The programmer opens the Target file,
*> but NEVER the Sort file.
200-Process-Loop-Para.
PERFORM UNTIL There-Are-No-More-Records
RETURN SortFile RECORD [INTO A-Working-Storage-Record]
AT END
MOVE "NO" to Are-There-More-Records
CLOSE TargetFile
NOT AT END
*> The record RETURNed from SortFile can be
*> processed before moving it to Target-Rec. Some of the
*> sorted records may be deleted or not processed at
*> this point.
MOVE the processed record to Target-Rec
WRITE Target-Rec [FROM A-Working-Storage-Record]
END-READ
END-PERFORM.
MOVE "YES" to Are-There-More-Records
*> The SORT verb closes the SortFile. Any actions that follow the
*> sort verb may now begin.
References
SORT
Shelly, Advanced Structured COBOL, 5.89-5.97 [RIC QA76.73.C25 S423].
Philippakis and Kazmier, Advanced COBOL, 498-509 [RIC QA76.73.C25 P48 1982].
Stern & Stern, Structured COBOL Programming, 9th Ed., 597[ISBN 0-471-31881-7].
Uckan, File Processing in COBOL, 139-147 [659-667].
RELEASE
Shelly, Advanced Structured COBOL, 5.96-5.97 [RIC QA76.73.C25 S423].
Stern & Stern, Structured COBOL Programming, 9th Ed., 597[ISBN 0-471-31881-7].
Uckan, File Processing in COBOL, 145 [665].
RETURN
Shelly, Advanced Structured COBOL, 5.95-5.96 [RIC QA76.73.C25 S423].
Stern & Stern, Structured COBOL Programming, 9th Ed., 603 [ISBN 0-471-31881-7].
Uckan, File Processing in COBOL, 125 [645].
[1]
C:\CompSci\CS310\CobolSyn\Sort\SortVerb.doc- 1 -
[1]Originally written 24 March 1997. Rewritten in structured COBOL 28 & 30 Oct 2001. Diagram added 10 Dec 2002.