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

ArgsReturnsNotes
(int, int)intInteger comparison.
(decimal, decimal)intDecimal comparison.
(number, number)intNumeric fallback when types can’t be statically resolved.
(string, string)intOrdinal, case-insensitive string comparison (same rule the == operator uses).
(bool, bool)intfalse < 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.

Docs last synced: 2026-07-08
Screenshot viewer