action.skip

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:

  1. sort-file-n is a file described in the File Section with an SD.
  2. sort-key-n is a data item that has been declared as a key to a sort table, or sort file.
  3. alphabet-name is a user-defined word.
  4. proc-name is the name of a paragraph or section in the program.
  5. file-n is a file described in the File Section with an FD.

General Rules:

  1. Sort-file-1 must be described with an SD in the FILE SECTION.
  2. The USING and GIVING files must be described with ORGANIZATION LINE SEQUENTIAL, or ORGANIZATION BINARY SEQENTIAL, and must be described with an FD in the FILE SECTION.
  3. The order in which the KEY clauses are listed indicates the order in which the sort keys are applied. The first KEY listed is the primary key, the second key listed is the second key, etc...
  4. The ASCENDING KEY clause causes values to sort from lowest to highest.
  5. The DESCENDING KEY clause causes values to sort from highest to lowe st.
  6. The COLLATING SEQUENCE clause names the alphabet which is used for purposes of ordering KEY data in the SORT. Usage of the COLLATING SEQUENCE clause in a SORT statement overrides the naming of a PROGRAM COLLATING SEQUENCE in the object computer paragraph.
  7. WITH DUPLICATES IN ORDER is treated as commentary.
  8. The SORT statement writes duplicates in the order in which they are encountered.
  9. The file listed in the USING clause may not be OPEN when the SORT statement executes. The SORT statement OPEN's the file.
  10. The files listed in the GIVING clause may not be OPEN when the MERGE statement executes. The SORT statement OPEN's the file.
  11. The OUTPUT PROCEDURE is executed after all of the records have been SORT'ed into the temporary sort file.
  12. Inside the OUTPUT PROCEDURE, the RETURN statement is used to transfer records from the sort-file to the output file.
  13. When the OUTPUT PROCEDURE is finished, the SORT statement closes all files.
  14. The SORT statement updates file status variable that is defined for sort-file after performing OPEN, READ, WRITE, and CLOSE operations. File Status errors may be detected in DECLARATIVES.

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:

  1. sort-tbl is a data item with an occurs clause.
  2. data-name-n is a data item defined in the sort-tbl, subordinate to the OCCURS.
  3. alphabet-name is a user defined word.

General Rules:

  1. The WITH DUPLICATES IN ORDER phrase is treated as commentary.
  2. The COLLATING SEQUENCE phrase is treated as commentary.
  3. The Format 2 SORT statement does not require a sort file definition (SD) in the File Section.
  4. The effect of the Format 2 SORT statement is a re-ordering of the elements in sort-tbl, as specified by the ASCENDING/DESCENDING KEY phrase(s).
  5. The internal COBOL SORT checks for the setting of TMPDIR as the location for the storage of temporary SORT files. If TMPDIR is not set, then the behavior is OS Dependent. In some operating systems, this can limit the size of files that can be SORT'ed by the internal COBOL 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.