Rational Developer for System z
COBOL for Windows, Version 7.5, Language Reference


SORT statement

The SORT statement accepts records from one or more files, sorts them according to the specified keys, and makes the sorted records available either through an output procedure or in an output file. See also MERGE statement. The SORT statement can appear anywhere in the procedure division except in the declarative portion.

Format
Read syntax diagramSkip visual syntax diagram>>-SORT--file-name-1-------------------------------------------->
 
   .--------------------------------------------------.  
   |                                  .-------------. |  
   V                                  V             | |  
>----+----+--+-ASCENDING--+--+-----+----data-name-1-+-+--------->
     '-ON-'  '-DESCENDING-'  '-KEY-'                     
 
>--+-----------------------------------------+------------------>
   '-+------+--DUPLICATES--+----+--+-------+-'  
     '-WITH-'              '-IN-'  '-ORDER-'    
 
>--+---------------------------------------------------+-------->
   '-+-----------+--SEQUENCE--+----+-- alphabet-name-1-'  
     '-COLLATING-'            '-IS-'                      
 
            .-------------.                                                          
            V             |                                                          
>--+-USING----file-name-2-+-------------------------------------------------------+-->
   '-INPUT PROCEDURE--+----+--procedure-name-1--+-------------------------------+-'  
                      '-IS-'                    '-+-THROUGH-+--procedure-name-2-'    
                                                  '-THRU----'                        
 
             .-------------.                                                          
             V             |                                                          
>--+-GIVING----file-name-3-+-------------------------------------------------------+-><
   '-OUTPUT PROCEDURE--+----+--procedure-name-3--+-------------------------------+-'  
                       '-IS-'                    '-+-THROUGH-+--procedure-name-4-'    
                                                   '-THRU----'                        
 
file-name-1
The name given in the SD entry that describes the records to be sorted.

No pair of file-names in a SORT statement can be specified in the same SAME SORT AREA clause or the SAME SORT-MERGE AREA clause. File-names associated with the GIVING clause (file-name-3, ...) cannot be specified in the SAME AREA clause; however, they can be associated with the SAME RECORD AREA clause.

ASCENDING KEY and DESCENDING KEY phrases

This phrase specifies that records are to be processed in ascending or descending sequence (depending on the phrase specified), based on the specified sort keys.

data-name-1
Specifies a KEY data item on which the SORT statement will be based. Each such data-name must identify a data item in a record associated with file-name-1. The data-names following the word KEY are listed from left to right in the SORT statement in order of decreasing significance without regard to how they are divided into KEY phrases. The leftmost data-name is the major key, the next data-name is the next most significant key, and so forth. The following rules apply:

If file-name-3 references an indexed file, the first specification of data-name-1 must be associated with an ASCENDING phrase and the data item referenced by that data-name-1 must occupy the same character positions in this record as the data item associated with the major record key for that file.

The direction of the sorting operation depends on the specification of the ASCENDING or DESCENDING keywords as follows:

DUPLICATES phrase

If the DUPLICATES phrase is specified, and the contents of all the key elements associated with one record are equal to the corresponding key elements in one or more other records, the order of return of these records is as follows:

If the DUPLICATES phrase is not specified, the order of these records is undefined. For more information about use of the DUPLICATES phrase, see the related discussion of alternate indexes in the COBOL for Windows Programming Guide.

COLLATING SEQUENCE phrase

This phrase specifies the collating sequence to be used in alphanumeric comparisons for the KEY data items in this sorting operation.

The COLLATING SEQUENCE phrase has no effect for keys that are not alphabetic or alphanumeric.

The COLLATING SEQUENCE phrase is valid only when a single-byte ASCII code page is in effect.

alphabet-name-1
Must be specified in the ALPHABET clause of the SPECIAL-NAMES paragraph. alphabet-name-1 can be associated with any one of the ALPHABET clause phrases, with the following results:
STANDARD-1
The collating sequence is based on the character's hex value order.
STANDARD-2
The collating sequence is based on the character's hex value order.
NATIVE
The collating sequence indicated by the locale is selected.
EBCDIC
The EBCDIC collating sequence is used for all alphanumeric comparisons. (The EBCDIC collating sequence is in Appendix C. EBCDIC and ASCII collating sequences.)
literal
The collating sequence established by the specification of literals in the alphabet-name clause is used for all alphanumeric comparisons.
 

When the COLLATING SEQUENCE phrase is omitted, the PROGRAM COLLATING SEQUENCE clause (if specified) in the OBJECT-COMPUTER paragraph specifies the collating sequence to be used. When both the COLLATING SEQUENCE phrase and the PROGRAM COLLATING SEQUENCE clause are omitted, the collating sequence indicated by the COLLSEQ compiler option is used.

USING phrase

file-name-2, ...
The input files.

When the USING phrase is specified, all the records in file-name-2, ..., (that is, the input files) are transferred automatically to file-name-1. At the time the SORT statement is executed, these files must not be open. The compiler opens, reads, makes records available, and closes these files automatically. If EXCEPTION/ERROR procedures are specified for these files, the compiler makes the necessary linkage to these procedures.

All input files must be described in FD entries in the data division.

If the USING phrase is specified and if file-name-1 contains variable-length records, the size of the records contained in the input files (file-name-2, ...) must be neither less than the smallest record nor greater than the largest record described for file-name-1. If file-name-1 contains fixed-length records, the size of the records contained in the input files must not be greater than the largest record described for file-name-1. For more information, see the COBOL for Windows Programming Guide.

INPUT PROCEDURE phrase

This phrase specifies the name of a procedure that is to select or modify input records before the sorting operation begins.

procedure-name-1
Specifies the first (or only) section or paragraph in the input procedure.
procedure-name-2
Identifies the last section or paragraph of the input procedure.

The input procedure can consist of any procedure needed to select, modify, or copy the records that are made available one at a time by the RELEASE statement to the file referenced by file-name-1. The range includes all statements that are executed as the result of a transfer of control by CALL, EXIT, GO TO, PERFORM, and XML PARSE statements in the range of the input procedure, as well as all statements in declarative procedures that are executed as a result of the execution of statements in the range of the input procedure. The range of the input procedure must not cause the execution of any MERGE, RETURN, or SORT statement.

If an input procedure is specified, control is passed to the input procedure before the file referenced by file-name-1 is sequenced by the SORT statement. The compiler inserts a return mechanism at the end of the last statement in the input procedure. When control passes the last statement in the input procedure, the records that have been released to the file referenced by file-name-1 are sorted.

GIVING phrase

file-name-3, ...
The output files.

When the GIVING phrase is specified, all the sorted records in file-name-1 are automatically transferred to the output files (file-name-3, ...).

All output files must be described in FD entries in the data division.

If the output files (file-name-3, ...) contain variable-length records, the size of the records contained in file-name-1 must be neither less than the smallest record nor greater than the largest record described for the output files. If the output files contain fixed-length records, the size of the records contained in file-name-1 must not be greater than the largest record described for the output files. For more information, see the COBOL for Windows Programming Guide.

At the time the SORT statement is executed, the output files (file-name-3, ...) must not be open. For each of the output files, the execution of the SORT statement causes the following actions to be taken:

These implicit functions are performed such that any associated USE AFTER EXCEPTION/ERROR procedures are executed; however, the execution of such a USE procedure must not cause the execution of any statement manipulating the file referenced by, or accessing the record area associated with, file-name-3. On the first attempt to write beyond the externally defined boundaries of the file, any USE AFTER STANDARD EXCEPTION/ERROR procedure specified for the file is executed. If control is returned from that USE procedure or if no such USE procedure is specified, the processing of the file is terminated.

OUTPUT PROCEDURE phrase

This phrase specifies the name of a procedure that is to select or modify output records from the sorting operation.

procedure-name-3
Specifies the first (or only) section or paragraph in the output procedure.
procedure-name-4
Identifies the last section or paragraph of the output procedure.

The output procedure can consist of any procedure needed to select, modify, or copy the records that are made available one at a time by the RETURN statement in sorted order from the file referenced by file-name-1. The range includes all statements that are executed as the result of a transfer of control by CALL, EXIT, GO TO, PERFORM, and XML PARSE statements in the range of the output procedure. The range also includes all statements in declarative procedures that are executed as a result of the execution of statements in the range of the output procedure. The range of the output procedure must not cause the execution of any MERGE, RELEASE, or SORT statement.

If an output procedure is specified, control passes to it after the file referenced by file-name-1 has been sequenced by the SORT statement. The compiler inserts a return mechanism at the end of the last statement in the output procedure and when control passes the last statement in the output procedure, the return mechanism provides the termination of the sort and then passes control to the next executable statement after the SORT statement. Before entering the output procedure, the sort procedure reaches a point at which it can select the next record in sorted order when requested. The RETURN statements in the output procedure are the requests for the next record.

The INPUT PROCEDURE and OUTPUT PROCEDURE phrases are similar to those for a basic PERFORM statement. For example, if you name a procedure in an output procedure, that procedure is executed during the sorting operation just as if it were named in a PERFORM statement. As with the PERFORM statement, execution of the procedure is terminated after the last statement completes execution. The last statement in an input or output procedure can be the EXIT statement (see EXIT statement).

SORT special registers

The special registers, SORT-CORE-SIZE, SORT-MESSAGE, and SORT-MODE-SIZE, are equivalent to option control statement keywords in the sort control file. You define the sort control data set with the SORT-CONTROL special register.

Note: If you use a sort control file to specify control statements, the values specified in the sort control file take precedence over those in the special register.

SORT-MESSAGE special register
See SORT-MESSAGE.
SORT-CORE-SIZE special register
See SORT-CORE-SIZE.
SORT-FILE-SIZE special register
See SORT-FILE-SIZE.
SORT-MODE-SIZE special register
See SORT-MODE-SIZE.
SORT-CONTROL special register
See SORT-CONTROL.
SORT-RETURN special register
See SORT-RETURN.

Segmentation considerations

If a SORT statement is coded in a fixed segment, any input or output procedure referenced by that SORT statement must be either totally within a fixed segment or wholly contained in a single independent segment.

If a SORT statement is coded in an independent segment, any input or output procedure referenced by that SORT statement must be either totally within a fixed segment or wholly contained within the same independent segment as that SORT statement.


Terms of use | Feedback

Copyright IBM Corporation 1996, 2008.
This information center is powered by Eclipse technology. (http://www.eclipse.org)