Syntax

	$expr1.compare($expr2)

Description

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.

Parameter

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

Return value

Type: Number

Depending on result the function returns:

Examples

Numbers

	compare(1, 5)

Output: -1

Strings

	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.

Boolean values

	compare("true", "false")

Output: 1

Lists

	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.

Sorting with a comparison value result

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.