SORT Statement
The SORT
statement reorders the records in a file, or a table .
Format 1
The Format 1 SORT
statement reorders the records in a file.
SORT sort-file-1
{ ON {ASCENDING } KEY {sort-key-1} } ...
{DESCENDING}
[ WITH DUPLICATES IN ORDER ]
[ COLLATING SEQUENCE IS alphabet-name ]
{ USING {file-1} ... }
{ GIVING {file-2} ... }
{ INPUT PROCEDURE IS proc-name }
{ OUTPUT PROCEDURE IS proc-name }
Syntax:
sort-file-n
is a file described in the File Section with an SD.sort-key-n
is a data item that has been declared as a key to a sort table, or sort file.alphabet-name
is a user-defined word.proc-name
is the name of a paragraph or section in the program.file-n
is a file described in the File Section with an FD.
General Rules:
Sort-file-1
must be described with an SD in theFILE SECTION
.- The
USING
andGIVING
files must be described withORGANIZATION LINE SEQUENTIAL
, orORGANIZATION BINARY SEQENTIAL
, and must be described with an FD in theFILE SECTION
. - The order in which the
KEY
clauses are listed indicates the order in which the sort keys are applied. The firstKEY
listed is the primary key, the second key listed is the second key, etc... - The
ASCENDING KEY
clause causes values to sort from lowest to highest. - The
DESCENDING KEY
clause causes values to sort from highest to lowe st. - The
COLLATING SEQUENCE
clause names the alphabet which is used for purposes of orderingKEY
data in theSORT
. Usage of theCOLLATING SEQUENCE
clause in aSORT
statement overrides the naming of aPROGRAM COLLATING SEQUENCE
in the object computer paragraph. WITH DUPLICATES IN ORDER
is treated as commentary.- The
SORT
statement writes duplicates in the order in which they are encountered. - The file listed in the
USING
clause may not beOPEN
when theSORT
statement executes. TheSORT
statementOPEN
's the file. - The files listed in the
GIVING
clause may not beOPEN
when theMERGE
statement executes. The SORT statementOPEN
's the file. - The
OUTPUT PROCEDURE
is executed after all of the records have beenSORT
'ed into the temporary sort file. - Inside the
OUTPUT PROCEDURE
, theRETURN
statement is used to transfer records from the sort-file to the output file. - When the
OUTPUT PROCEDURE
is finished, the SORT statement closes all files. - The
SORT
statement updates file status variable that is defined for sort-file after performingOPEN
,READ
,WRITE
, andCLOSE
operations. File Status errors may be detected inDECLARATIVES
.
Format 2
The Format 2 SORT
statement reorders the elements in a table.
SORT sort-tbl
[ ON ASCENDING/DESCENDING KEY data-name-1 ... ]
[ WITH DUPLICATES IN ORDER ]
[ COLLATING SEQUENCE IS alphabet-name ]
Syntax:
sort-tbl
is a data item with an occurs clause.data-name-n
is a data item defined in thesort-tbl
, subordinate to theOCCURS
.alphabet-name
is a user defined word.
General Rules:
- The
WITH DUPLICATES IN ORDER
phrase is treated as commentary. - The
COLLATING SEQUENCE
phrase is treated as commentary. - The Format 2
SORT
statement does not require a sort file definition (SD) in the File Section. - The effect of the Format 2
SORT
statement is a re-ordering of the elements insort-tbl
, as specified by theASCENDING
/DESCENDING KEY
phrase(s). - The internal
COBOL SORT
checks for the setting ofTMPDIR
as the location for the storage of temporarySORT
files. IfTMPDIR
is not set, then the behavior is OS Dependent. In some operating systems, this can limit the size of files that can beSORT
'ed by the internalCOBOL SORT
.
Code Sample:
SORT USING/GIVING
IDENTIFICATION DIVISION.
PROGRAM-ID. SORT-1.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
*INPUT FILE
SELECT MEMBER-FILE ASSIGN TO "MEMBERSHIP.TXT"
ORGANIZATION IS LINE SEQUENTIAL
ACCESS IS SEQUENTIAL
FILE STATUS IS MEMBERSHIP-STAT.
*OUTPUT FILE
SELECT MEMBER-LIST ASSIGN TO "MEMBERLIST.TXT"
ORGANIZATION IS LINE SEQUENTIAL
ACCESS IS SEQUENTIAL
FILE STATUS IS MEMBERLIST-STAT.
*SORTFILE (SD)
SELECT MEMBER-SORT ASSIGN TO "SORT-WORK".
DATA DIVISION.
FILE SECTION.
FD MEMBER-FILE.
01 MEMBER-INFOPIC X(40).
FD MEMBER-LIST.
01 SORTED-MEMBER-INFOPIC X(40).
SD MEMBER-SORT.
01 SORT-DATA.
05 MEMBER-FIRST-NAME PIC X(10).
05 MEMBER-LAST-NAME PIC X(20).
05 YEAR-JOINED PIC X(4).
05 MEMBER-RANK PIC X(6).
WORKING-STORAGE SECTION.
77 MEMBERLIST-STAT PIC XX.
77 MEMBERSHIP-STATPIC XX.
PROCEDUREDIVISION.
PRODUCT-LIST-SORT.
SORT MEMBER-SORT
ON ASCENDING KEY MEMBER-LAST-NAME
ON DESCENDING KEY MEMBER-RANK
WITH DUPLICATES IN ORDER
USING MEMBER-FILE
GIVING MEMBER-LIST.
SORT Table
IDENTIFICATION DIVISION.
PROGRAM-ID. SORT-2.
ENVIRONMENT DIVISION.
DATA DIVISION.
WORKING-STORAGE SECTION.
77 MEMBERLIST-STAT PIC XX.
77 MEMBERSHIP-STAT PIC XX.
77 LINE-NUMBER PIC 99 VALUE 10.
77 CTR PIC 99 VALUE 0.
77 DUMMY PIC X.
01 MEMBER-TABLE.
05 MEMBER-1 PIC X(20) VALUE "SAMUEL JOHNSON ".
05 MEMBER-2 PIC X(20) VALUE "THOMAS JEFFERSON ".
05 MEMBER-3 PIC X(20) VALUE "GEORGE WASHINGTON ".
05 MEMBER-4 PIC X(20) VALUE "SAMUEL ADAMS ".
05 MEMBER-5 PIC X(20) VALUE "ABRAHAM LINCOLN ".
05 MEMBER-6 PIC X(20) VALUE "WOODROW WILSON ".
05 MEMBER-7 PIC X(20) VALUE "FRANKLIN ROOSEVELT ".
05 MEMBER-8 PIC X(20) VALUE "DWIGHT EISENHOWER ".
01 MEMBER-TABLE-RED REDEFINES MEMBER-TABLE.
05 PATRIOT-TBL OCCURS 8 TIMES.
10 FIRST-NAME PIC X(10).
10 LAST-NAME PIC X(10).
*
PROCEDURE DIVISION.
MEMBER-SORT.
SORT PATRIOT-TBL
ON ASCENDING KEY FIRST-NAME.
PERFORM VARYING CTR FROM 1 BY 1 UNTIL CTR > 8
DISPLAY FIRST-NAME(CTR) LINE LINE-NUMBER COL 10
DISPLAY LAST-NAME(CTR) LINE LINE-NUMBER COL 20
ADD 1 TO LINE-NUMBER
END-PERFORM.
DISPLAY "SORT-2 FINISHED!" LINE 20 COL 10.
ACCEPT DUMMY LINE 20 COL 30.
STOP RUN.
SORT INPUT/OUTPUT Procedures
IDENTIFICATION DIVISION.
PROGRAM-ID. SORT-3.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
*INPUT FILE
SELECT MEMBER-FILE ASSIGN TO "MEMBERSHIP.TXT"
ORGANIZATION IS LINE SEQUENTIAL
ACCESS IS SEQUENTIAL
FILE STATUS IS MEMBERSHIP-STAT.
*OUTPUT FILE
SELECT MEMBER-LIST ASSIGN TO "MEMBERLIST.TXT"
ORGANIZATION IS LINE SEQUENTIAL
ACCESS IS SEQUENTIAL
FILE STATUS IS MEMBERLIST-STAT.
*SORTFILE (SD)
SELECT MEMBER-SORT ASSIGN TO "SORT-WORK".
DATA DIVISION.
FILE SECTION.
FD MEMBER-FILE.
01 MEMBER-INFO PIC X(40).
FD MEMBER-LIST.
01 SORTED-MEMBER-INFO PIC X(40).
SD MEMBER-SORT.
01 SORT-DATA.
05 MEMBER-FIRST-NAME PIC X(10).
05 MEMBER-LAST-NAME PIC X(20).
05 YEAR-JOINED PIC X(4).
05 MEMBER-RANK PIC X(6).
WORKING-STORAGESECTION.
77 MEMBERLIST-STAT PIC XX.
88 EOF-MEMBERLIST VALUE "10".
77 MEMBERSHIP-STAT PIC XX.
88 EOF-MEMBERSHIP VALUE "10".
77 OUTPUT-SORT-AT-END PIC X.
88 EOF-SORT-FILE VALUE "Y".
77 DUMMY PIC X.
PROCEDURE DIVISION.
MEMBER-SORT-PROC.
SORT MEMBER-SORT
ON ASCENDING KEY MEMBER-LAST-NAME
ON DESCENDING KEY MEMBER-RANK
WITH DUPLICATES IN ORDER
INPUT PROCEDURE INPUT-PROC
OUTPUT PROCEDURE OUTPUT-PROC.
DISPLAY "SORT-3 FINISHED!" LINE 10 COL 10.
ACCEPT DUMMY LINE 10 COL 30.
STOP RUN.
INPUT-PROC SECTION.
INPUT-PROCESS.
OPEN INPUT MEMBER-FILE.
READ MEMBER-FILE NEXT RECORD.
PERFORM UNTIL EOF-MEMBERLIST
RELEASE SORT-DATA FROM MEMBER-INFO
READ MEMBER-FILE NEXT RECORD
AT END MOVE "10" TO MEMBERLIST-STAT
END-READ
END-PERFORM.
CLOSE MEMBER-FILE.
EXIT SECTION.
OUTPUT-PROC SECTION.
OUTPUT-PROCESS.
OPEN OUTPUT MEMBER-LIST.
PERFORM UNTIL EOF-SORT-FILE
RETURN MEMBER-SORT
AT END MOVE "Y" TO OUTPUT-SORT-AT-END
NOT AT END
WRITE SORTED-MEMBER-INFO FROM SORT-DATA
END-RETURN
END-PERFORM.
CLOSE MEMBER-LIST.
EXIT SECTION.