groovy Spaceship Operator Basic usage


Example

the spaceship operator returns -1 when the left operator is smaller, 0 when the operators are equal and 1 otherwise:

assert 10 <=> 20 == -1
assert 10 <=> 10 ​== 0
assert 30 <=> 10 == 1

assert 'a' <=> 'b' == -1
assert 'a' <=> 'a'​== 0
assert 'b' <=> 'a' == 1​​

It is equivalent to the Comparable.compareTo method:

assert 10.compareTo(20) == (10 <=> 20)
assert 'a'.compareTo('b') == ('a' <=> 'b')