SORT—Sort Data

The SORT primary command puts data in a specified order.

Syntax

Read syntax diagramSkip visual syntax diagramSORT.ZFIRST.ZLASTlabelalabelbXNXsort_field
sort_field
Read syntax diagramSkip visual syntax diagramADstart_colend_col
labela, labelb
Labels identifying the start and end of the group of lines to be sorted.

For more information about using labels to identify a group of lines, see Labels and line ranges.

X
Sorts only lines that are excluded.
NX
Sorts only lines that are not excluded.
sort_field
Specifies the field to be used in sorting data. You can specify up to five sort fields using these operands:
A
Specifies ascending order. It can either precede or follow the column specification.
D
Specifies descending order. It can either precede or follow the column specification.
start_col
Defines the starting column of the field that is to be compared. It must be within the current boundaries.
end_col
Defines the ending column of the field that is to be compared. It must be within the current boundaries. If it is not supplied, then the ending column is the current right boundary. For more information on boundaries, see Edit boundaries.

If you specify several fields, you must specify both the starting and ending columns of each field. The fields cannot overlap. If you supply the sort order for one field, you must supply it for all fields.

Description

SORT operates in two different modes, based on the hexadecimal mode status. If hexadecimal mode is on, the data is ordered according to its hexadecimal representation. If hexadecimal mode is off, data is sorted in the collating sequence defined for the national language being used.

Sorting data without operands

For SORT with no operands, the editor compares the data within the current boundaries character by character, and then orders it line by line in the proper collating sequence. It ignores data outside the current boundaries during both operations. Therefore only the data inside the current boundaries is changed. Labels, excluded lines, line numbers, and change, error, and special line flags are considered associated with the data, and therefore point to the same data fields after the sort as they did before the sort.

For example, if you issue a CHANGE ALL that changes the first, third, and sixth lines in a data set, these lines are flagged with the change flag, ==CHG>. If you then issue a SORT command that results in the former lines 1, 3, and 6 becoming the first, second and third lines of the sorted file, the changed line flags would now exist on the first, second and third lines of the sorted data set.

It is important to properly set the boundaries before issuing SORT. SORT is a powerful tool for editing data that may be formatted in multiple columns. You can set the boundaries, for example, to the first half of a record and sort one column of data. Then you can set the boundaries to the last half of the record and sort a second column of data.

Limiting the SORT command

Sorting is limited to data within the current boundaries. You can specify up to five sort fields by labeling starting and ending columns. You can also identify each field as having data sorted in either ascending or descending order.

Optionally, you can limit sorting to a range of lines by specifying the labels of the first and last lines of the range. You can also limit sorting to either excluded or non-excluded lines.

If you have labels or line ranges that are between the labels or line ranges specified with SORT, you can keep SORT from rearranging them by:
  • Excluding them before you enter SORT
  • Using the NX operand to sort only lines that are not excluded

For more information, see the definition of the NX operand and EXCLUDE—Exclude Lines from the Display.

Sorting DBCS data

When sorting data that contains DBCS character strings, you must ensure that no DBCS string crosses the boundaries. Also, all records must have the same format at the boundaries, although the format of the left and right boundaries can differ.

If a boundary divides a DBCS character, or if all records do not have the same format at the boundaries, the result is unpredictable.

Examples

This form of the SORT command sorts in ascending order. The start-column is the left boundary and the end-column is the right boundary:

SORT

This form of the SORT command sorts in descending order. The start-column is the left boundary and the end-column is the right boundary:

SORT D

This form of the SORT command sorts in ascending order. The start-column is column 5 and the end-column is the right boundary:

SORT 5

This form of the SORT command sorts in descending order. The start-column is column 5 and the end-column is the right boundary:

SORT 5 D