# Ruby Spaceship \<=\> Operator

DevFeed: [Ruby Spaceship \<=\> Operator](<https://devfeed.tech/articles/ruby-spaceship-operator-21065.md>)

Original publisher: [Read original article](<https://jakeyesbeck.com/2016/02/28/ruby-spaceship-operator/>)

Published: 2016-02-28T12:00:00Z

Content type: tutorial

Language: en

Sources: [Jake Yesbeck](<https://devfeed.tech/sources/jake-yesbeck.md>)

Topics: [Ruby](<https://devfeed.tech/topics/ruby.md>), [Programming](<https://devfeed.tech/topics/programming.md>), [Sorting](<https://devfeed.tech/topics/sorting.md>), [Code](<https://devfeed.tech/topics/code.md>)

Tags: [code](<https://devfeed.tech/tags/code.md>), [programming](<https://devfeed.tech/tags/programming.md>), [ruby](<https://devfeed.tech/tags/ruby.md>), [sorting](<https://devfeed.tech/tags/sorting.md>)

## AI overview

A practical introduction to Ruby's <=> spaceship operator, explaining its -1, 0, and 1 comparison results and showing how to use it for ascending, reverse, grouped, and compound sorting.

## Source excerpt

Adhering to the law of trichotomy, the <=> operator (sometimes called the "Spaceship Operator") works by comparing two elements and returning a -1, 0, or 1. While the original mathematical criteria applies to only real numbers, many programming languages implement the law of trichotomy as a general comparison between equivalent types. Basics At first glance, the return value of the <=> operator can be a bit confusing. A simple way to remember the significance of these return values is to break an expression down from left to right: 1. < -> -1 if a < b, then -1 is returned 2. = -> 0 if a = b, then 0 is returned 3. > -> 1 if a > b, then 1 is returned Example: > a = 3 > b = 5 > a <=> b # => -1 > a = 3 > b = 3 > a <=> b # => 0 > a = 10 > b = 3 > a <=> b # => 1 Sorting The <=> operator can be used alone for comparison or its contract honored within a block following the sort method. By default, the <=> operator behaves as described above: list = [8, 3, 1, 4, 0, 3] list.sort { |a, b| a <=> b } # => [0, 1, 3, 3, 4, 8] Following this pattern, it is easy to sort a list in reverse by swapping operand positions: list = [8, 3, 1, 4, 0, 3] list.sort { |a, b| b <=> a } # => [8, 4, 3, 3, 1, 0] The sort method is extendable beyond explicit use of the <=> operator. A block passed to sort must only return either -1, 0, or 1 for sorting to work effectively. If the same list were to be ordered in odds then evens: list = [8, 3, 1, 4, 0, 3] list.sort { |a, _| a.odd? ? -1 : 1 } # => [3, 3, 1, 4, 8, 0] Also, since -1, 0, and 1 are simple integers, creating compound <=> blocks is possible. If the same list were to be sorted odds then evens, with all odd and even numbers sorted in ascending order, it might look like this: list = [8, 3, 1, 4, 0, 3] list.sort do |a, b| if a.odd? if b.odd? # both are odd, default <=> behaviour is used a <=> b else -1 # a < b end else # a is even if b.even? # both are even, default <=> behaviour is used a <=> b else 1 # a > b end end end # => [1, 3, 3, 0, 4, 8]