In the SORT statement, you specify the key on which the records will be sorted. The key must be defined in the record description of the records to be sorted. In the following example, notice that SORT-GRID-LOCATION and SORT-SHIFT are defined in the Data Division before they are used in the SORT statement.
DATA DIVISION.
·
·
·
SD SORT-FILE. 01 SORT-RECORD. 05 SORT-KEY. 10 SORT-SHIFT PIC X(1). 10 SORT-GRID-LOCATION PIC X(2). 10 SORT-REPORT PIC X(3). 05 SORT-EXT-RECORD. 10 SORT-EXT-EMPLOYEE-NUM PIC X(6). 10 SORT-EXT-NAME PIC X(30). 10 FILLER PIC X(73). PROCEDURE DIVISION.
·
·
·
SORT SORT-FILE ON ASCENDING KEY SORT-GRID-LOCATION SORT-SHIFT INPUT PROCEDURE 600-SORT3-INPUT OUTPUT PROCEDURE 700-SORT3-OUTPUT.
·
·
·
To sort on more than one key, as shown in the example above, list the keys in descending order of importance. The example also shows the use of an input procedure and an output procedure. Use an input procedure if you want to process the records before you sort them, and use an output procedure if you want to further process the records after you sort them.
There is no maximum number of keys, as long as the total length of the keys does not exceed 2000 bytes.
Key data items may be floating-point for both the SORT (and MERGE) operations. If the key is an external floating-point item, it is treated as character data, which means that the sequence in which the records are sorted depends on the collating sequence used. If the key is an internal floating-point item, the sequence will be in numeric order.
Key data items may be of class date-time for both the SORT (and MERGE) operations. In general items of class date-time are only sorted as if they were date, time, and timestamp items for those items that match iSeries DDS date, time and timestamp formats; in all other cases they are treated as character data. Items of class date-time that are treated as character data ignore the collating sequence in effect during the SORT or MERGE. For more information about using date-time data types in ILE COBOL programs, refer to Working with Date-Time Data Types.
The following is the list of DDS data types that are treated as date-time items for the purpose of sorting:
Key data items may have null-values for both SORT (and MERGE) operations. In a database file, the null value occupies the highest value in the collating sequence. To be able to SORT (and MERGE) null-capable files containing null values, however, you need to first define the file as null-capable by specifying the ALWNULL keyword in the ASSIGN clause.
You can sort records on EBCDIC, ASCII, or another collating sequence. The default collating sequence is EBCDIC or the PROGRAM COLLATING SEQUENCE you specified in the Configuration Section. You can override the collating sequence named in the PROGRAM COLLATING SEQUENCE by using the COLLATING SEQUENCE phrase of the SORT statement. Consequently, you can use different collating sequences for multiple sorts in your program.
You can also specify the collating sequence that a program will use when it is run, at the time that you compile the ILE COBOL source program. You can specify the collating sequence to be used, through the SRTSEQ and LANGID parameters of the CRTCBLMOD and CRTBNDCBL commands. Refer to Specifying National Language Sort Sequence in CRTCBLMOD for a description of how to specify the collating sequence at compile time. You can override the collating sequence specified at compile time by specifying the PROGRAM COLLATING SEQUENCE clause in the OBJECT-COMPUTER paragraph or by using the COLLATING SEQUENCE phrase of the SORT statement.
When you sort an ASCII file, you have to request the ASCII collating sequence. To do this, use the COLLATING SEQUENCE alphabet-name phrase of the SORT statement, where alphabet-name has been defined in the SPECIAL-NAMES paragraph as STANDARD-1. You can also specify this in the PROGRAM COLLATING SEQUENCE clause of the OBJECT-COMPUTER paragraph if no COLLATING SEQUENCE phrase is specified on the SORT or MERGE statement that overrides it.
(C) Copyright IBM Corporation 1992, 2005. All Rights Reserved.