SORT.COMPARE()

SORT.COMPARE()

Top  Previous  Next

 

The SORT.COMPARE() function compares two items using a specified set of comparison rules.

 

 

Format

 

SORT.COMPARE(string1, string2, mode {, no.case})

 

where

 

string1is the first string to be compared.
string2is the second string to be compared.
modeidentifies the manner in which the comparison is to be performed.
no.casea Boolean value indicating whether the comparison should be case insensitive. This argument is retained for compatibility with earlier releases but case insensitivity can be specified in the mode value.

 

 

The SORT.COMPARE() function compares string1 and string2 according to the sort rules specified by the mode value which may be specified as a numeric value or by use of the tokens from the SYSCOM KEYS.H include record shown below.

 

Mode 0SRT$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.
Mode 1SRT$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.
Mode 2SRT$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.
Mode 3SRT$RIGHT.FLOAT. The same as mode 1 except that the test for numeric items allows non-integer values.
Mode 4SRT$STRICT. Similar to a compound sort but numerically equivalent elements such as "28" and "028" to be treated as being different.

 

Other additive mode flags are:

SRT$DESCENDINGThe result of the comparison operation is reversed.
SRT$NOCASEPerform a case insensitive comparison.        

 

In all cases, the no.case argument can be used to specify that alphabetic items should be treated as case insensitive, effectively replacing lower case letters with their upper case equivalents.

 

The SORT.COMPARE() function returns

-1string1 comes before string2
0string1 is equal to string2
1string1 comes after string2

 

 
Examples

 

Using modes the modes indicated below, the effect of sorting GC5, ND620, GC41 and CD631 would be

Mode 0CD631, GC41, GC5, ND620
Mode 1GC5, GC41, CD631, ND620
Mode 2CD631, GC5, GC41, ND620

 

 

See also:

COMPARE(), !SORT()