SORT()

SORT()

Top  Previous  Next

 

The SORT() function creates a sort variable.

 

 

Format

 

SORT(key.count, {MAT} flags)

 

where

 

key.countspecifies the number of keys in the sort. Up to 32 keys may be specified.

 

flagsis the name of a single dimensional array of sort key mode flags prefixed with MAT. For a single key sort, flags may be specified as a scalar variable or an expression.

 

 

 

The SORT() function is used at the start of a sort operation to create a sort variable that controls the sort process. The function returns a sort variable that can be used in subsequent sort related operations.

 

The flags value is formed from the items below. The token names shown are defined in the SYSCOM KEYS.H include record.

 

At most one of the following sort styles:

0

SRT$LEFT

A simple left justified comparison, examining corresponding characters from the start of each string until either a difference is found or the end of both strings has been reached.

1

SRT$RIGHT

A simple right justified comparison in which the shorter item is effectively padded with leading spaces and the resultant strings are compared character by character from the start until either a difference is found or the end of both strings has been reached. If both strings can be treated as integer values, possibly with a leading sign character, a numeric comparison is performed.

2

SRT$COMPOUND

A compound sort in which the two strings are considered to be formed from a series of alternating numeric and non-numeric elements. Numeric elements are sorted into numerical value, non-numeric elements are sorted into collating sequence order. If the first element is numeric, it may have an optional leading sign character. Sign characters appearing later in the strings are treated as being non-numeric characters.

3

SRT$RIGHT.FLOAT

The same as mode 1 except that the test for numeric items allows non-integer values. The same as mode 1 except that the test for numeric items allows non-integer values.

 

Any combination of the following sort options:

16

SRT$DESCENDING

The result of the comparison operation is reversed.

32

SRT$UNIQUE

Duplicate key values are ignored.

64

SRT$NOCASE

Case insensitive sort.

128

SRT$DATA

Sort has the optional data element that will be used to hold a data string associated with each item in the sort. This flag is only significant in first flags item and controls use of the SORTADD data argument.

 

 

Examples

 

The following example shows a simple sort with a single key and no data. The first loop enters the data. The second loop displays the sorted results.

 

SORTVAR = SORT(1, SRT$LEFT)

LOOP

  INPUT X

UNTIL X = ""

  SORTADD SORTVAR, X

REPEAT

 

LOOP

  X = SORTNEXT(SORTVAR)

UNTIL STATUS()

  DISPLAY X
REPEAT

 

 

The next example shows a two key sort using a descending order numeric value representing a date as the first key and a case insensitive left aligned sorted client id as the second key. The client name is attached to the keys as a data element.

 

DIM KEYS(2)

KEYS(1) = SRT$RIGHT + SRT$DESCENDING + SRT$DATA

KEYS(2) = SRT$NOCASE

SORTVAR = SORT(2, MAT KEYS)

 
SELECT ORD.F

LOOP

  READNEXT ORD.ID ELSE EXIT

  READ ORD.REC FROM ORD.F, ORD.ID THEN

     KEYS(1) = ORD.REC<O.DATE>

     KEYS(2) = ORD.REC<O.CLIENT>

     SORTADD SORTVAR, MAT KEYS, ORD.REC<O.NAME>
  END

REPEAT

 

LOOP

  NAME = SORTNEXT(SORTVAR, MAT KEYS)

UNTIL STATUS()

  DISPLAY OCONV(KEYS(1), 'D2DMY'), KEYS(2), NAME

REPEAT

 

The above example is broadly equivalent to use of a query

LIST ORDERS DATE @ID NAME BY.DSND DATE BY @ID

 

 

See also:

Sorting, SORTADD, SORTCLEAR, SORTDATA(), SORTNEXT()