Sorting the data

You can sort your data by using the SORT primary command. With this command, you can sort the data in any of the following ways:

  • On up to 5 specified column ranges (CHAR, HEX, or LHEX display formats only).
  • On up to 5 fields by specifying the field reference numbers (TABL display format only).
  • Into the KEY or SLOT NUMBER sequence for the data set (KSDS, VRDS, and RRDS VSAM data sets).
Note: SORT requires that all data set records be memory-resident. If the entire data set can not fit into your region storage, an error is reported and the SORT execution aborted. If necessary, increase the region size to contain the entire data set.

You can specify whether you want the data sorted in ascending (the default) or descending sequence and, in Edit, you can restrict the sorting to excluded records or not-excluded records only.

To sort by a column range:

  1. Display your data in CHAR, HEX or LHEX format.
  2. Enter the SORT primary command, followed by up to five pairs of column range values. For example:
    SORT 56 70 3 22
    sorts a data set into ascending order, using the range 56-70 as the first key and range 3-22 as the second key.

To sort by field references:

  1. Display your data in TABL format (requires that you have specified a template).
  2. Enter the SORT primary command, followed by up to five field references. For example:
    SORT #6 D #3 D
    sorts a data set into descending order, using field #6 as the first key and field #3 as the second key.

To restore a list to the KEY or SLOT NUMBER order (KSDS, VRDS, and RRDS VSAM data sets):

  1. Display your KSDS, VRDS, or RRDS VSAM data set in any display format.
  2. Sort the data away from the KEY or SLOT NUMBER order by issuing a SORT command, by moving or adding records or by editing the key field (KSDS only). If you have edited your records, issue the SAVE command.
  3. Restore your data to its KEY or SLOT NUMBER order by issuing the SORT command followed by the KEY parameter, for example:
    SORT KEY.
    sorts a data set into descending order, using field #6 as the first key and field #3 as the second key.

Examples

  • If you are not using a template:
    SORT 23 36
    Sorts all records on columns 23 to 36 in ascending order.
    SORT 77 80 D NX
    (Edit only.) Sorts not-excluded records on columns 77 to 80 in descending order (as if excluded records did not exist).
    SORT 77 80 D 23 36 3 21
    Sorts all records on columns 77 to 80 (primary sort key) in descending order, then on columns 23 to 36 (secondary sort key) in ascending order, then on columns 3 to 21 (minor sort key) in ascending order.
    SORT KEY
    Restores the records in a KSDS, VRDS, or RRDS VSAM data set into their original keyed sequence.

    SORT KEY behaves differently for RRDS and KSDS files. For RRDS files the VSAM slot value is used as the sort key while KSDS files use the key field. If new records are added to an RRDS file they are allocated a slot value such that they sort to the end of the file when SORT KEY is performed. New records added to the KSDS are sorted on the new key value and the new records sorted to the appropriate position within the file.

  • If you are using a template (TABL display format):
    SORT #7 D #6 #3
    Sorts all records on the field with field reference number 7 (primary sort key) in descending order, then on the field with field reference number 6 (secondary sort key) in ascending order, then on the field with field reference number 3 (minor sort key) in ascending order,
    SORT #6 X
    (Edit only.) Sorts all excluded records on the field with field reference number 6 in ascending order (as if non-excluded records did not exist).

Related topics