$expr1.compare($expr2)
Returns the comparison result of expr1 and expr2. The comparison result is less than 0, equal to 0, greater than 0 if expr1 is less than, equal to, or greater than expr2.
| Name | Type | Description | Mandatory | Default |
|---|---|---|---|---|
| expr1 | Number/string/boolean/business object | An element to be compared with expr2. See Comparison operators. |
yes | |
| expr2 | Number/string/boolean/business object | An element to be compared with expr1. See Comparison operators. |
yes |
Type: Number
Depending on result the function returns:
expr1 and expr2 are the same sizeexpr1 is greater than expr2expr1 is smaller than expr2 compare(1, 5)
Output: -1
compare("z", "a")
Output: 25
When comparing strings, not only is the result less than, greater than, or equal to 0 output, but additionally defines how far apart the strings are.
compare("true", "false")
Output: 1
compare(list(3 , 2, 1) , list(1, 2, 3))
Output: 1
Lists are compared element by element until one element is greater or less for a pair of elements, see Comparison operators.
list("z", "a", "f")
.sort(x -> y -> compare($x, $y))
Output: A list with the elements ["a", "f", "z"].
See the example "Comparison with comparison function" in Sort.