util.Strings.collateNumeric

Compares two strings using locale collation rules and sequences of numerical digits.

Syntax

util.Strings.collateNumeric(
    s1 STRING,
    s2 STRING
 ) RETURNS INTEGER
  1. s1 is the string to compare to s2.
  2. s2 is the string to compare to s1.

Usage

The util.Strings.collateNumeric() method compares two strings by following the collation rules defined by the current application locale, and by interpreting any sequence of numerical digits as a positive integer value.

For details about the collation rules that can be used, read the OS documentation about setlocale/LC_COLLATE category.

Sequence of numerical digits are grouped to form whole numbers that will be compared. For example, "10M" is greater than "1M" (10>1), and "10.5M" is lower than "10.56M" (10=10, 5<56). Note that decimal and negative numbers are not detected. For example, comparing "-10M" and "+10M" will produce a positive integer.

When s1<s2, the method returns a negative integer, when s1>s2, it returns positive integer and when s1==s2, it returns zero. If one of the strings is NULL, it is considered as the lowest possible value. If both strings are NULL, they are considered as equal.

Example

Source "collate.4gl":

IMPORT util

MAIN
    CALL test("10M","1M")
    CALL test("150M","100M")
    CALL test("10.5M","10.56M")
    CALL test("-10M","+10M")  -- Minus/plus signs are ignored!
END MAIN

FUNCTION test(s1 CHAR(10), s2 CHAR(10))
    DISPLAY s1, " / ", s2, " => ", util.Strings.collateNumeric(s1,s2)
END FUNCTION
Output:
10M        / 1M         =>           1
150M       / 100M       =>           1
10.5M      / 10.56M     =>          -1
-10M       / +10M       =>           2

Note that the last line shows a positive number, because minus/plus signs are ignored.