1 file organization considerations for sorting – Compaq COBOL AAQ2G1FTK User Manual

Page 280

Advertising
background image

Using the SORT and MERGE Statements
9.1 Sorting Data with the SORT Statement

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–2 Using the SORT and MERGE Statements

Advertising