The <=> comparison operator is often referred to as the spaceship operator. It performs a 3-way comparison (returning less, equal, or greater) on the two operands.
The spaceship operator (so named because of its appearance) is used to compare items for sorting in various languages (such as Perl, Ruby, and Groovy). The standard definition is that:
a <=> b
is less than zero ifa < b
a <=> b
is zero ifa == b
a <=> b
is greater than zero ifa > b
The spaceship operator can also be used to provide default implementations of the <
, >
, and even ==
operators if the language (or libraries) is clever enough.