Compare
Compares two values and returns -1 if the first is smaller, 0 if
equal, and 1 if larger. Both arguments must be the same type.
Category: Math
Returns: int
Signature
Compare(a, b)
The runtime requires both arguments to be the same type. There is no
implicit widening. This makes it the escape hatch for ordering bools
(false < true) and comparing strings by ordinal position, since the
<, <=, >, >= operators only work with numbers.
Overloads
| Args | Returns | Notes |
|---|---|---|
(int, int) | int | Integer comparison. |
(decimal, decimal) | int | Decimal comparison. |
(number, number) | int | Numeric fallback when types can’t be statically resolved. |
(string, string) | int | Ordinal, case-insensitive string comparison (same rule the == operator uses). |
(bool, bool) | int | false < true. |
Examples
Compare(a, b) < 0 // a is less than b
Compare(tagA, tagB) > 0 // string ordering: which tag sorts first
Compare(isVIP, isVIP2) // bool ordering: false < true
Compare(score1, score2) // rank two scores for sorting
Notes
Compare is the only way to order non-numeric types, since <, <=,
>, and >= only accept numbers. Use it to gate logic on string or
bool ordering. String comparison is case-insensitive, so
Compare("Apple", "apple") returns 0. Mixing types (comparing a
string to a number, for example) is an error: both arguments must be
the same type.