Sorting a file

The SORT verb can be used by a programmer to sort a file in a new sequence. It can be implemented in a stand alone program or as part of another program. When it is incorporated with another program it can be coded in the initialization stage before the rest of the program is executed, at some point during the processing of the program, or in the termination stage when processing is complete. The sort verb sorts a file so it should not be part of the record by record processing routine.

FORMAT:

This format is for the simple version of the SORT where an input file is sorted using a sort file and an output file is produced. The sort processing is accomplished entirely by the SORT verb code.

  		SORT sort-file-name
		     ON {ASCENDING } KEY data-name-1...
		        {DESCENDING}
		     USING file-name-2
			GIVING file-name-3
The sort verb can sort numeric or non-numeric data in ascending or descending order and it can sort on multiple keys so you can have sort on something within something (for example sort on department within branch).

The sort verb uses three files to do the sort and each one must be defined with a SELECT:

NOTE: the sort work file is assigned differently in different implementations of COBOL and different systems - you need to check specifications for the environment you are working in.

In the DATA DIVISION, the input and output file are set up with FDs, but the sort work file is set up with a SD (Sort Description).

FD  UNSORTED-FILE....
01  UNSORTED-RECORD           PIC X(50).
SD  SORT-WORK-FILE....
01  SORT-WORK-RECORD.
    05  FIELD-TO-SORT-ON      PIC X(5).
    05  FILLER                PIC X(45).
FD  SORTED-FILE....
01  SORTED-RECORD	      PIC X(50).
The simple SORT with the USING and GIVING clauses:

  1. Opens the files
  2. Reads records from the input file
  3. Writes (releases) records to the sort work file
  4. Sorts the sort file according to the keys specified in the SORT statement - in the example above the key is FIELD-TO-SORT-ON
  5. Reads (returns) the files from the sort work file and writes them to the output file
  6. Closes the files
PROCEDURE DIVISION.
MAINLINE.
    SORT SORT-WORK-FILE
	ON ASCENDING KEY FIELD-TO-SORT-ON
	USING UNSORTED-FILE
	GIVING SORTED-FILE.
    STOP RUN.
If the program is a standalone sort, the procedure division would be completed with a STOP RUN. If the processing to be done involves sorting a file at the beginning of the program and then processing the sorted records, the MAINLINE would have one of the following looks.

Example #1:

 MAINLINE.
     SORT SORT-WORK-FILE
		ON ASCENDING KEY FIELD-TO-SORT-ON
		USING UNSORTED-FILE
		GIVING SORTED-FILE.
	PERFORM A-100-INITIALIZE.
	PERFORM B-200-PROCESS.
	PERFORM C-300-WRAPUP.
	STOP RUN.
   A-100-INITIALIZE.
	OPEN INPUT SORTED-FILE
          OUTPUT...
  B-100-PROCESS.
	READ SORTED-FILE
		AT END
		   MOVE "NO " TO MORE-RECS.
	PERFORM B-200-LOOP
		UNTIL MORE-RECS = "NO ".

Example #2:

 MAINLINE.
     PERFORM A-100-INITIALIZE.
     PERFORM B-200-PROCESS.
     PERFORM C-300-WRAPUP.
     STOP RUN.
 A-100-INITIALIZE.
     SORT SORT-WORK-FILE
	ON ASCENDING KEY FIELD-TO-SORT-ON
	USING UNSORTED-FILE
	GIVING SORTED-FILE.
     OPEN INPUT SORTED-FILE
          OUTPUT...
 B-100-PROCESS.
     READ SORTED-FILE
	 AT END
	     MOVE "NO " TO MORE-RECS.
     PERFORM B-200-LOOP
	UNTIL MORE-RECS = "NO ".
If the programmer wants to control processing prior to writing the records on the sort work file an INPUT PROCEDURE can be substituted for the USING clause. For example, if the programmer wanted to select certain records to be sorted, the selection could be done in the input procedure prior to releasing the record to the sort file. If the programmer wants to control processing of the sorted records prior to writing them on the output file this can be done by substituting and OUTPUT PROCEDURE for the GIVING clause. These features give the programmer using the SORT verb the capacity to select or extract certain records while doing the SORT. There are several new concepts involved in this processing:

Example #1:

The following example uses an INPUT PROCEDURE to select records during the sort. Only the selected records will be sorted and put on the specified output file.

MAINLINE.
     PERFORM A-100-INITIALIZE.
	SORT SORT-WORK-FILE
		ASCENDING KEY IS FIELD-TO-BE-SORTED-ON
		INPUT PROCEDURE IS B-100-SORT-ROUTINE
		GIVING SELECTED-SORTED-FILE.
     PERFORM C-100-TERMINATE.
     STOP RUN.
A-100-INITIALIZE.
	OPEN INPUT UNSORTED-FILE.
B-100-SORT-ROUTINE.
	READ UNSORTED-FILE
		AT END
		  MOVE "NO " TO MORE-RECS.
	PERFORM B-200-SELECT-RECORDS
		UNTIL MORE-RECS = "NO ".
B-200-SELECT-RECORDS.
	IF CODEZ = "X"
		MOVE UNSORTED-RECORD TO SORT-WORK-RECORD
		RELEASE SORT-WORK-REC.
	READ UNSORTED-FILE
		AT END
		  MOVE "NO " TO MORE-RECS.
C-100-TERMINATE SECTION.
	CLOSE UNSORTED-FILE.

Example #2:

This is an alternative to the approach in example #1. Instead of using the INPUT PROCEDURE to process the records, the program selects the records to be processed and writes them to a file which is used as input to the sort. There are four files in this example:

MAINLINE.
     PERFORM A-100-INITIALIZE.
     PERFORM B-100-PROCESS.
     PERFORM C-100-TERMINATE.
     STOP RUN.
A-100-INITIALIZE.
     OPEN INPUT UNSORTED-FILE
	  OUTPUT SELECTED-UNSORTED-FILE.
B-100-PROCESS.
     READ UNSORTED-FILE
	 AT END
	     MOVE "NO " TO MORE-RECS.
     PERFORM B-200-LOOP
	UNTIL MORE-RECS = "NO ".
B-200-LOOP.
     IF CODEZ = "X"
	WRITE SELECTED-UNSORTED-RECORD
             FROM UNSORTED-RECORD.
     READ UNSORTED-FILE
          AT END
             MOVE "NO " TO MORE-RECS.
C-100-TERMINATE.	
     CLOSE UNSORTED-FILE
	   SELECTED-UNSORTED-FILE.
     SORT SORT-WORK-FILE
	  ASCENDING KEY FIELD-TO-BE-SORTED
	  USING SELECTED-UNSORTED-FILE
	  GIVING SELECTED-SORTED-FILE.

Example #3:

An example of a program that uses an output procedure.

MAINLINE.
	PERFORM A-100-INITIALIZE.
	SORT SORT-WORK-FILE
		ASCENDING KEY FIELD-TO-BE-SORTED
		USING UNSORTED-FILE
		OUTPUT PROCEDURE B-100-SORT-ROUTINE.
	PERFORM C-100-TERMINATE.
        STOP RUN.
A-100-INITIALIZE.
	OPEN OUTPUT SORTED-FILE.
B-100-SORT-ROUTINE SECTION.
	RETURN SORT-WORK-FILE
		AT END
	        MOVE "YES" TO PROCESS-COMPLETE.
	PERFORM B-200-LOOP
		UNTIL PROCESS-COMPLETE = "YES".
B-200-LOOP.
	processing to be done on the record - possibly a calculation to generate 
        new data for the output file - make sure that the new data is in the 
        SORTED-RECORD before doing the WRITE or do a WRITE...FROM...
	WRITE SORTED-RECORD
	RETURN SORT-WORK-FILE
		AT END
		   MOVE "YES" TO PROCESS-COMPLETE.
C-100-TERMINATE.
	CLOSE SORTED-FILE.