


The SORT statement sorts records according to selected key fields. Record content can be modified before and after the actual sort process using INPUT PROCEDURE and OUTPUT PROCEDURE.
SORT sort-file
{ KEY AREA IS key-table }
{ ON {ASCENDING } KEY {key-name} } ...
{DESCENDING}
[ WITH DUPLICATES IN ORDER ]
[ COLLATING SEQUENCE IS alpha-name ]
{ INPUT PROCEDURE IS proc-name }
{ USING {in-file} ... }
{ OUTPUT PROCEDURE IS proc-name }
{ GIVING {out-file} ... }
start-proc [ {THRU } end-proc ]
{THROUGH}
Syntax Rules
1. Sort-file names a sort file described by an SD entry in the Data Division.
2. Key-table must name a data item that is not located in the record for sort-file. Key-table may not be subordinate to an OCCURS clause, nor may it be reference modified.
3. Key-table must reference a data item whose size is an even multiple of 7. Key-table is processed as if it had the following structure:
01 KEY-TABLE.
03 SORT-KEY OCCURS N TIMES.
05 KEY-ASCENDING PIC X COMP-X.
05 KEY-TYPE PIC X COMP-X.
05 KEY-OFFSET PIC XX COMP-X.
05 KEY-SIZE PIC XX COMP-X.
05 KEY-DIGITS PIC X COMP-X.
Typically, programs will declare key-table with a similar format.
4. Key-name is a data item in the record description associated with sort-file. It may not be subordinate to an OCCURS clause nor may it be a group item containing variable occurrence data items.
5. Alpha-name is an alphabet-name defined in the SPECIAL-NAMES paragraph of the Environment Division.
6. In-file and out-file are files described by FD entries in the Data Division. They may not be sort files.
7. Start-proc and end-proc are paragraph or section names in the Procedure Division.
8. A SORT statement may not appear in Declaratives or in the input or output procedure of a SORT or MERGE statement.
9. If sort-file contains variable length records, in-file records must not be smaller than the smallest record in sort-file nor larger than the largest. If sort-file contains fixed length records, in-file records may not be larger than the size of sort-file's records.
10. If out-file contains variable length records, sort-file records must not be smaller than the smallest record in out-file nor larger than the largest. If out-file contains fixed length records, sort-file records may not be larger than the size of out-files records.
11. If sort-file contains more than one record description, key-name need appear in only one of them. The character positions referenced by key-name are used as the key for all the file's records.
12. If out-file is an indexed file, the first key-name must be ASCENDING and must specify the same character positions in its record as the primary record key for out-file.
13. THRU is an abbreviation for THROUGH.
General Rules
1. The SORT statement sorts records received from the INPUT PROCEDURE or found in the in-files. It then either makes these sorted records available to the OUTPUT PROCEDURE or writes them to each out-file.
2. If sort-file contains fixed length records, any shorter in-file records are space-filled on the right to match the record size.
3. If out-file contains fixed length records, any shorter sort-file records are space-filled on the right to match the record size.
4. The first key-name is the major key, and the next key-name is the next most significant key. This pattern continues for each key-name specified.
5. The ASCENDING phrase specifies that key values are to be ordered from lowest to highest. The DESCENDING phrase specifies the reverse ordering. Once ASCENDING or DESCENDING is specified, it applies to each key-name until another ASCENDING or DESCENDING adjective is encountered.
6. Use the KEY AREA option when you do not know the specifics of the sort key until the program is run. You can use this to allow users to enter sort key specifications, typically in conjunction with some form of data dictionary.
7. Your program must fill in a table of information that describes the sort keys. This table, key-table, should have the format described by Syntax Rule 3 above. The number of sort keys is determined by the number of occurrences in the table. The keys are listed in order of precedence: table entry 1 describes the highest precedence key, table entry 2 the second highest, and so on. If you need to process a variable number of keys, use a variable-size table (by using OCCURS DEPENDING ON).
8. For each key, you must specify the following information:
KEY-ASCENDING: This should be 0 or 1. Enter 1 to have an ascending sort sequence, 0 for descending.
KEY-TYPE: Describes the underlying data format. The allowed values are listed in the next rule.
KEY-OFFSET: Describes the distance (in standard character positions) from the beginning of the sort record to the beginning of the key field. The first field in a sort record is at offset 0.
KEY-SIZE: Describes the size of the key field in standard character positions.
KEY-DIGITS: This is used only for numeric keys. It describes the number of digits contained in the key (counting digits on both sides of the decimal point).
9. The KEY-TYPE field uses a code to describe the type and internal storage format of the data item. Select from the following values:
| 0
| Numeric edited
|
| 1
| Unsigned numeric (DISPLAY)
|
| 2
| Signed numeric (DISPLAY, trailing separate)
|
| 3
| Signed numeric (DISPLAY, trailing combined)
|
| 4
| Signed numeric (DISPLAY, leading separate)
|
| 5
| Signed numeric (DISPLAY, leading combined)
|
| 6
| Signed COMP-2
|
| 7
| Unsigned COMP-2
|
| 8
| Unsigned COMP-3
|
| 9
| Signed COMP-3
|
| 10
| COMP-6
|
| 11
| Signed binary (COMP-1, COMP-4, COMP-X)
|
| 12
| Unsigned binary (COMP-1, COMP-4, COMP-X)
|
| 13
| Signed native (COMP-5, COMP-N)
|
| 14
| Unsigned native (COMP-5, COMP-N)
|
| 15
| Floating point (FLOAT, DOUBLE)
|
| 16
| Alphanumeric
|
| 17
| Alphanumeric (justified)
|
| 18
| Alphabetic
|
| 19
| Alphabetic (justified)
|
| 20
| Alphanumeric edited
|
| 21
| Not used
|
| 22
| Group |
10. The results are undefined if you provide invalid data in the key-table. If you fail to specify any keys (by specifying a table whose size is zero), you receive a file error on sort-file. Under the default file status codes, this is file error 94 with a secondary status of 63.
11. For nonnumeric keys, the COLLATING SEQUENCE phrase establishes the ordering. If this phrase is omitted, the NATIVE collating sequence is used. For numeric keys, the ordering is specified by the algebraic value of the key.
12. The DUPLICATES phrase affects the return order for records whose key-name values are equal.
a) When there is a USING phrase, the return order is the same as the order of appearance of in-file names in the SORT statement. Within a given in-file, the order is that in which the records are accessed from that file.
b) When there is an INPUT PROCEDURE, the return order is the same as the order in which records were released. If the DUPLICATES phrase is not used, the return order for records with equal key values is unpredictable.
13. The execution of a SORT statement consists of three distinct phases. These are:
a) Records are made available to the sort-file. This is achieved either by executing RELEASE statements in the input procedure or by implicit execution of READ statements for each in-file. When this phase starts, in-file must not be open. When it finishes, in-file will not be open.
b) The sort-file is sequenced according to the KEY phrase and the DUPLICATES clause. No processing of in-files or out-files takes place during this phase.
c) The records in sort-file are made available in sorted order. The sorted records are either written to the out-files or are made available to an output routine through execution of a RETURN statement. When this phase starts, out-file must not be open. When it finishes, out-file will be closed.
14. If the INPUT PROCEDURE phrase is used, the named procedure is executed by the SORT statement according to the rules for the PERFORM verb. This procedure must make records available to the input phase of the sort operation by executing RELEASE statements. When this procedure returns, the sort operation proceeds to the sequencing phase. The range of the input procedure may not cause the execution of a MERGE, RETURN, or SORT statement.
15. If the USING phrase, is specified, all records in each in-file are transferred to sort-file. For each in-file, the following actions occur:
a) The file is opened as if it were the object of an OPEN INPUT statement with no options.
b) The records are obtained and released to the sort operation. Each record is obtained as if a READ statement with the NEXT and AT END phrases had been executed. For relative files, the RELATIVE KEY data item is undefined at the end of this phase.
c) The file is closed as if it were the object of a CLOSE statement with no options. This occurs prior to the sequencing of sort-file.
These implicit functions are performed such that any associated USE procedures are executed. These USE procedures must not access in-file or its record area.
16. If an output procedure is specified, control passes to it after the sort-file has been sequenced. Control passes to the output procedure according to the rules of the PERFORM statement. The output procedure must execute RETURN statements to retrieve the sorted records. When the output procedure returns, the SORT statement terminates and control passes to the next executable statement. The range of the output procedure must not execute any MERGE, RELEASE, or SORT statements.
17. If the GIVING phrase is used, all the sorted records are written to each out-file. For each of these files, the following steps occur:
a) Out-file is opened as if it were the object of an OPEN OUTPUT statement with no options.
b) The sorted records are returned and written to the file. The records are written as if a WRITE statement without any options had been executed. For a relative file, the value of the RELATIVE KEY data item is updated to reflect the record number written.
c) The file is closed as if it were the object of a CLOSE statement without any options.
These implicit functions are performed such that any associated USE procedures are executed. Such a USE procedure may not refer to out-file or its record area. On the first attempt to write beyond the externally defined boundaries of the file, any applicable USE procedure is executed. If control is returned from that USE procedure, or no USE procedure is applicable, the processing of that out-file is terminated.
18. If the SORT statement is in a fixed segment, the range of any input and output procedures must be contained completely in the fixed segments and no more than one independent segment. If the MERGE statement is in an independent segment, the range must be completely contained in the fixed segments and the same independent segment.
19. The SORT statement updates the value of the sort-file's FILE STATUS data item.
20. Only one SORT may be active at a time. See also "CANCEL SORT."
More: