


1. SORT is used to order records according to a set of key fields (sort keys). Records may be stored in sequential, relative, or indexed files, or records may be acquired by use of an INPUT PROCEDURE. Once ordered, the record set may be further processed by use of an OUTPUT PROCEDURE, or the records may be written directly to the named output file(s).
2. SORT is most often used to order records stored in disk files. However, by using an INPUT PROCEDURE you can acquire records from other input sources such as output from batch processes, internal application data structures, or screen input.
3. SORT creates a special temporary disk file (the sort file) as a work space for collecting, sorting, and holding ordered records. The sort file is defined by an SD entry in the DATA DIVISION. The sort file record definition must immediately follow the SD entry and must include definitions for each sort key used, except when the KEY AREA phrase is used. You can place temporary files used by the SORT verb in a specified directory--see the SORT-DIR configuration variable in Appendix H, Book 4, "Appendices." The sort file is removed when the SORT statement completes.
4. Most SORT procedures involve the reading, sorting, and writing of records stored in disk files. These disk I/O processes can be relatively slow and, therefore, the SORT process may take a lot of time. With careful application design, the power and utility of the SORT function easily justifies the cost in time.
5. The three basic steps of the SORT procedure are:
a) Acquiring and placing the records to be sorted in the sort file:
When the USING phrase is used, SORT opens each named input file, reads the data records, one at a time, into the sort file and closes the input file. Input files must not be open before the SORT statement begins.
When an INPUT PROCEDURE is used the RELEASE verb is used to pass records to the sort file. If records are acquired from disk files, it is the responsibility of the input procedure to open, read, process, RELEASE each individual record, and close the files. For more information, see the entry in this section for "RELEASE Statement."
b) Sorting the records:
Using the sort file, and a set of temporary files, records are sorted according to the key phrase, the DUPLICATES clause, and the COLLATING clause. See also the configuration variables in Appendix H, Book 4, "Appendices," under Sort-Dir, Sort-Memory, and Sort-Reserve.
c) Disposition of the sorted records:
When the GIVING phrase is used, the sorted records are written to the named permanent output file(s).
When an OUTPUT PROCEDURE is used, the sorted records are made available to the output procedure for processing and writing to a permanent file(s). The output procedure uses the verb RETURN to acquire the ordered records from the sort file. It is the responsibility of the output procedure to open, write, and close the output file(s). For more information regarding the use of the RETURN verb, see its entry in this section.
6. A SORT statement may not appear in a DECLARATIVES section or in an INPUT or OUTPUT PROCEDURE that is part of a SORT statement (nesting of SORT statements is not permitted).
7. The KEY AREA phrase is a means for defining the sort keys at runtime, as the application is running. When you use KEY AREA, it is not required that the sort file record descriptor contain entries for potential sort keys. Definition of the sort key(s) in the sort file is handled internally by the SORT routine using the key table. See syntax rules 2 and 3 and general rules 6 through 10.
8. If the KEY AREA phrase is not used, the sort keys must be defined in the record description of the sort file.
9. Use of INPUT PROCEDURE or OUTPUT PROCEDURE requires that all file I/O operations and record disposition be handled by the input or output procedure. This means that the input and output procedures must explicitly perform the OPEN, READ, RELEASE (input), RETURN (output), WRITE, and CLOSE actions. As with any I/O management, the procedure should consider and account for the handling of all I/O related errors.
10. Use the DUPLICATES phrase when you want duplicate records to be sequenced in the same order that they are read in or RELEASEd. Duplicate records are those that have identical key values. In the absence of the DUPLICATES phrase sequencing of duplicate records is not predictable (see general rule 12).
11. Use the COLLATING SEQUENCE phrase to alter the ordering of nonnumeric keys. The named collating sequence must be defined in the SPECIAL-NAMES paragraph of the ENVIRONMENT DIVISION. In the SPECIAL-NAMES paragraph the user may define a unique character order, or the user may select one of the four predefined character sequences: STANDARD-1, STANDARD-2, NATIVE, and EBCDIC. See section 4.1.3, " SPECIAL-NAMES Paragraph."
If no COLLATING SEQUENCE phrase is used, the default collating sequence is used. The default collating sequence is whatever is native to the operating system (usually the same as the predefined type NATIVE).
12. Use the STATUS variable to hold the execution status of the SORT operation. The status variable is named in the SELECT/ASSIGN phrase of the FILE-CONTROL paragraph of the INPUT-OUTPUT SECTION. See section 4.2.1, " FILE-CONTROL Paragraph."
For a complete list and description of file status codes, see Appendix J, Book 4, "Appendices."
13. To specify the disk directory in which SORT will place any temporary files, set the Sort-Dir runtime configuration variable, located in the runtime configuration file.
Extended version of code example 2:
For simplicity, only one input file will be used.
IDENTIFICATION DIVISION.
PROGRAM-ID. SAMPLE-FILE-SORT.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
*SORT input file
SELECT ATHLETIC-SHOES-LIST
ASSIGN TO ....
*SORT output file
SELECT PRODUCT-LIST
ASSIGN TO ....
*sort file (SD)
SELECT PRODUCT-SORT-FILE
ASSIGN TO ....
DATA DIVISION.
FILE SECTION.
FD ATHLETIC-SHOES-LIST.
01 A-SHOE-RECORD PIC X(38).
FD PRODUCT-LIST.
01 B-SHOE-RECORD PIC X(38).
SD PRODUCT-SORT-FILE.
01 SORT-DATA.
05 MODEL-NAME PIC X(10).
05 MODEL-TYPE PIC X(3).
05 MODEL-NUMBER PIC X(3).
05 STOCK-NUMBER PIC X(7).
05 DESIGN-YEAR PIC 99.
05 UNIT-COST PIC 999V99.
05 UNIT-PRICE PIC 999V99.
05 FACTORY-NUM PIC 999.
WORKING-STORAGE SECTION.
01 FLAGS.
05 SHOE-LIST-EMPTY PIC X VALUE "N".
88 NO-MORE-SHOE-RECORDS VALUE "Y".
05 SORT-FILE-EMPTY PIC X VALUE "N".
88 NO-MORE-SORT-RECORDS VALUE "Y".
01 HONG-KONG-NUMBER PIC 99.
01 TAIWAN-NUMBER PIC 99.
...
PROCEDURE DIVISION.
PRODUCT-LIST-SORT.
*temporary SD file used by sort
SORT PRODUCT-SORT-FILE
*major sort key
ON ASCENDING KEY MODEL-TYPE
*minor sort key
ON DESCENDING KEY MODEL-NUMBER
*duplicates sorted in the order acquired
WITH DUPLICATES IN ORDER
INPUT PROCEDURE IS WEED-PRODUCT-LIST
OUTPUT PROCEDURE IS UPDATE-PRODUCT-LIST.
WEED-PRODUCT-LIST SECTION.
OPEN-LIST-FILE.
OPEN INPUT ATHLETIC-SHOES-LIST.
PERFORM WEED-LIST
UNTIL NO-MORE-SHOE-RECORDS.
CLOSE ATHLETIC-SHOES-LIST.
GO TO EXIT-WEED-PRODUCT-LIST.
WEED-LIST.
READ ATHLETIC-SHOES-LIST NEXT
AT END MOVE "Y" TO SHOE-LIST-EMPTY
NOT AT END
*stock numbers beginning with "X" are obsolete
*do not RELEASE
IF STOCK-NUMBER(1:1) = "X" THEN
NEXT SENTENCE
ELSE
*otherwise release the record to SORT
RELEASE SORT-DATA
END-IF.
EXIT-WEED-PRODUCT-LIST.
EXIT.
UPDATE-PRODUCT-LIST SECTION.
CREATE-PRODUCT-LIST.
OPEN OUTPUT PRODUCT-LIST.
PERFORM UPDATE-RECORD
UNTIL NO-MORE-SORT-RECORDS.
CLOSE PRODUCT-LIST.
GO TO EXIT-UPDATE-PRODUCT-LIST.
UPDATE-RECORD.
RETURN PRODUCT-SORT-FILE INTO SORT-DATA
AT END MOVE "Y" TO SORT-FILE-EMPTY
NOT AT END
IF FACTORY-NUM = HONG-KONG-NUMBER THEN
MOVE TAIWAN-NUMBER TO FACTORY-NUM
END-IF
WRITE B-SHOE-RECORD FROM SORT-DATA.
EXIT-UPDATE-PRODUCT-LIST.
EXIT.