HP OpenVMS Systems Documentation

Content starts here

HP COBOL
User Manual


Previous Contents Index


Chapter 9
Using the SORT and MERGE Statements

This chapter includes the following information about using the SORT and MERGE statements to sort and merge records for sequential, line sequential (Alpha and I64 only), relative, and indexed files:

9.1 Sorting Data with the SORT Statement

The SORT statement provides a wide range of sorting capabilities and options. To establish a SORT routine, you do the following:

  1. Declare the sort file with an Environment Division SELECT statement.
  2. Use a Data Division Sort Description (SD) entry to define the sort file's characteristics.
  3. Use a Procedure Division SORT statement.

The following program segments demonstrate SORT program coding:

SELECT Statement (Environment Division)


SELECT SORT-FILE ASSIGN TO "SRTFIL"

An SD File Description Entry (Data Division)


SD  SORT-FILE.
01  SORT-RECORD.
     05 SORT-KEY1    PIC X(5).
     05 SOME-DATA    PIC X(25).
     05 SORT-KEY2    PIC XX.

Note

You can place the sort file anywhere in the FILE SECTION, but you must use a Sort Description (SD) level indicator, not a File Description (FD) level indicator. Also, you cannot use the SD file for any other purpose in the COBOL program.

SORT Statement (Procedure Division)


SORT SORT-FILE
     ASCENDING KEY S-NAME
     USING NAME-FILE
     GIVING NEW-FILE.

The SORT statement names a sort file, sort keys, an input file, and an output file. An explanation of sort keys follows.

Sorting Concepts

Records are sorted based on the data values in the sort keys. Sort keys identify the location of a record or the ordering of data. The following example depicts unsorted employee name and address records used for creating mailing labels:

Smith, Joe 234 Ash St. New Boston NH 04356
Jones, Bill 12 Birch St. Gardner MA 01430
Baker, Tom 78 Oak St. Ayer MA 01510
Thomas, Pete 555 Maple St. Maynard MA 01234
Morris, Dick 21 Harris St. Acton ME 05670

If you sort the addresses in the previous example in ascending order using the zip code as the sort key, the mailing labels are printed in the order shown in the following example:

          SORT KEY
Thomas, Pete 555 Maple St. Maynard MA 01234
Jones, Bill 12 Birch St. Gardner MA 01430
Baker, Tom 78 Oak St. Ayer MA 01510
Smith, Joe 234 Ash St. New Boston NH 04356
Morris, Dick 21 Harris St. Acton ME 05670

Also, records can be sorted on more that one key at a time. If you need an alphabetical listing of all employees within each state, you can sort on the state code first (major sort key) and employee name second (minor sort key).

For example, if you sort the file in ascending order by state and last name, the employee names and addresses appear in the order shown in the following example:

SORT KEY
(minor)
      SORT KEY
(major)
 
Baker, Tom 78 Oak St. Ayer MA 01510
Jones, Bill 12 Birch St. Gardner MA 01430
Thomas, Pete 555 Maple St. Maynard MA 01234
Morris, Dick 21 Harris St. Acton ME 05670
Smith, Joe 234 Ash St. New Boston NH 04356

9.1.1 File Organization Considerations for Sorting

You can sort any file regardless of its organization; furthermore, the organization of the output file can differ from that of the input file. For example, a sort can have a sequential input file and a relative output file. In this case, the relative key for the first record returned from the sort is 1; the second record's relative key is 2; and so forth. However, if an indexed file is described as output in the GIVING or OUTPUT PROCEDURE phrases, the first sort key associated with the ASCENDING phrase must specify the same character positions specified by the RECORD KEY phrase for that file.

Sections 9.1.2, 9.1.3, and 9.1.4 describe the ASCENDING and DESCENDING KEY phrases, the USING and GIVING phrases, and the INPUT PROCEDURE and OUTPUT PROCEDURE phrases for sorting.

9.1.2 Specifying Sort Parameters with the ASCENDING and DESCENDING KEY Phrases

Use the Data Division ASCENDING and DESCENDING KEY phrases to specify your sort parameters. The order of data names determines the sort hierarchy; that is, the major sort key is the first data name entered, while the minor sort key is the last data name entered.

In the following example, the hierarchy of the sort is SORT-KEY-1, SORT-KEY-2, SORT-KEY-3.


SORT SORT-FILE
    ASCENDING KEY SORT-KEY-1 SORT-KEY-2
    DESCENDING KEY SORT-KEY-3


Previous Next Contents Index