SORT - Sort the Edit Data



[ criteria-1 ]  ... [ criteria-5 ]

[ line-control-range ]


[ X | NX ]

[ U | NU ]

[ MX | DX ]




You may specify up to five sort fields. Each criteria is of the following format:

start-col [ end-col ]  [ direction [case] ]

start-col        The left column of the sort key.  If not specified, 1 is assumed.

end-col        The right column of the sort key.  If no end-col is specified, the longest line length is used.

direction        A for ascending sort; D for descending sort.

case        C for a case-sensitive sort; T for case-insensitive sort.

See the information on Stable sorting below regarding the automatic addition of a sequence number to each sort key.


The range of lines which are to be processed by the command.  Line control ranges provide a powerful tool to customize the range of lines to be processed.   The full syntax and allowable operands which make up a line control range are discussed in "Line Control Range Specification".  Refer to that section of the documentation for details.


UNIQ requests SORT to drop duplicate records from a group of lines having the same sort key(s).  If no sort keys are supplied, SORT UNIQ will use the assumed defaults of 1 and max line length and will remove duplicate lines based on that assumed key. That is, in the absence of criteria options, SORT will use a key equal to the maximum line length (padding with spaces if needed) to determine if records are unique or not.  SORT will report on the number of duplicates dropped during sorting.  

Because a UNIQ sort could result in fewer records returned than originally provided, if the original records being sorted were in non-contiguous areas, such as spans of excluded lines or lines having a given line tag, some of those areas could end up with fewer records or no records at all.  Depending on the distribution of such non-contiguous lines and the values of the data lines, the exact distribution of data across such non-contiguous lines after duplicates are removed may or may not be repeatable.  You should check your SORT UNIQ output carefully when using it in a non-contiguous data environment to be sure you have the desired results.

Note: The processing done by SORT UNIQ in removing duplicate records is different than that used by DELETE DUP.  See DELETE - Delete Selected Lines for more information.

Note: The command SORT UNIQ will delete all non-unique lines.  If you want to delete all unique lines, leaving only lines having duplicate keys, first use SORT MARKUNIQ with the desired sort-key operands, and then issue the primary command DELETE ALL U.


The MARKUNIQ keyword will cause all lines having unique sort keys to be flagged as User lines (U lines).  When you use SORT MARKUNIQ, no lines are deleted the way that SORT UNIQ can do.  Recall that when a line is marked as a User line, it is flagged with a  |  vertical line in the "gap column" between the sequence number and column 1 of your data.  See Working with User lines for more information.

If you wanted the non-unique lines (lines with duplicate sort keys) marked instead of the unique ones, you can use the TU line command to toggle the User state of your file after issuing SORT MARKUNIQ.  The block line command TUU can be used to reverse the User status of a block of lines.  If you want the User status of the entire file to be reversed, you can place a line command of TU/ on line 1 of your file and press Enter.  Keep in mind that the action performed by TU and TUU is repeatable, so that if you decide later you want the User flags back the way they were, just issue the same TU or TUU command a second time for the same line range you did at first.

X | NX

Specifies a subset of the line range to be processed.   X requests only excluded lines are to be examined, NX requests only non-excluded lines are to be examined.   If neither X or NX are specified, all lines in the range will be examined.

U | NU

Specifies a subset of the line range to be processed.   U requests only User lines are to be processed, NU requests only non-User lines are to be processed.   If neither U or NU are specified, all lines in the range will be processed.


MX requests that all lines which are processed be excluded from the display following command processing.  

MX = make excluded.


DX requests that lines which are processed and which, if excluded, would normally be made visible, be left in their excluded status.  DX = Don't change exclusion status.        


For SORT with no operands, the editor will default to an ascending sort on columns 1 through the maximum line length (padding with blanks if needed); if only a start-col operand is provided for one of the criteria-n, then the key will be from the specified column through the maximum line length.

The A | D operand may precede or follow the column operands, or may be omitted if an ascending sort (the default) is desired.

If the Case modifier is not present on the A/D operand, then the SORT will use the default set by the CASE option.


SORT 1 10 A

This will sort the data in ascending order of columns 1 thru 10.  


This will sort the data in ascending order of columns 22 thru max line length.  

SORT 5 12 DC  21 30 AT

This will sort the data in descending, case sensitive order on columns 5 through 12, secondary sort on columns 21 through 30 in ascending case insensitive order.

SORT 2 22 A :ABC

This will sort the data in ascending order on columns 2 through 22. The line-range-operand specifies :ABC which indicates only lines tagged with the tag :ABC are to be sorted.  These lines could be scattered throughout the file, they will be sorted without disturbing the order of the remaining non-tagged lines in the file.

Difference from IBM's ISPF SORT command

Unlike ISPF, SPFLite's SORT implicitly changes excluded lines to unexcluded during the course of a SORT X command.  To achieve the effect of an ISPF command SORT X, it is necessary to specify this as SORT X DX instead.

The SORT Command performs stable sorting of data

The SORT primary command performs a stable sorting operation; this maintains the original ordering of data lines when the sort keys are considered equal.  This is achieved by SPFLite internally adding the sequence number of each line as a hidden, low-order sort key on every line.  This happens automatically and no user action is required.

Application Note: Performing a Dictionary Sort

As a consequence of stable sorting, it is now possible to perform a “dictionary” sort, in which words which are all-caps appear first, then words with an initial cap, and finally words in lower case appear last, where the same underlying word is involved.  A dictionary sort is done by sorting a line range first in case-sensitive mode, then in case-insensitive mode.  This in turn is done by specifying SORT C and then SORT T, or by issuing a CASE C, then SORT, then CASE T, then SORT again.


Original lines:

000001 BBB

000002 aaa

000003 Bbb

000004 Aaa

000005 bbb

000006 AAA

After  SORT C:

000001 AAA

000002 Aaa

000003 BBB

000004 Bbb

000005 aaa

000006 bbb

After  SORT T:

000001 AAA

000002 Aaa

000003 aaa

000004 BBB

000005 Bbb

000006 bbb

Now, the data is in dictionary-sorted order.

Created with the Personal Edition of HelpNDoc: Write EPub books for the iPad