Combined Comparison/"Spaceship" Operator (<=>) in JavaScript

alternative to combined comparison operator (=) in .sort

return returns from a method. It can only be used inside a method. There is no method in your code, therefore you get an error … and even if there were a method, the code wouldn't do what you want, because return returns from the method, not the block.

To return a value from a block, use next:

array.sort! {|a, b| 
if b < a
next -1
elsif b > a
next 1
else
next 0
end
}

However, unlike in C, if/then/else is an expression in Ruby, not a statement. (Actually, everything is an expression in Ruby, there are no statements.) This means that everything, including a conditional expression, returns a value. For if/then/else the value being returned is the one from the branch that was taken.

So, instead of returning from each of the branches separately, we can just return the vlue of the whole if expression:

array.sort! {|a, b| 
next if b < a
-1
elsif b > a
1
else
0
end
}

And since the return value of a block (just like the return value of a method) is implicitly the last value evaluated inside the block, we can just say:

array.sort! {|a, b| 
if b < a
-1
elsif b > a
1
else
0
end
}

next is mostly useful to break out early of a block in a guard-clause style or to flatten a deeply nested conditional:

array.sort! {|a, b| 
next -1 if b < a
next 1 if b > a
0
}

Note that case would probably more appropriate than if here:

array.sort! {|a, b| 
case
when b < a
-1
when b > a
1
else
0
end
}

How does Ruby's sort method work with the combined comparison (spaceship) operator?

a <=> b will return -1 if a belongs before b, 0 if they're equal, or 1 if a should follow b.

b <=> a will return -1 if b belongs before a, 0 if they're equal, or 1 if b should follow a.

Since you are reversing the order, the output should be reversed, just like the - operator, for example. 3-5 is -2, and 5-3 is 2.

array.sort { |b, a| b <=> a } is equal to array.sort { |a, b| a <=> b } because the first argument is before the spaceship, and the second is after. Ruby doesn't care what the name of the variable is.

What is the Ruby = (spaceship) operator?

The spaceship operator will return 1, 0, or −1 depending on the value of the left argument relative to the right argument.

a <=> b :=
if a < b then return -1
if a = b then return 0
if a > b then return 1
if a and b are not comparable then return nil

It's commonly used for sorting data.

It's also known as the Three-Way Comparison Operator. Perl was likely the first language to use it. Some other languages that support it are Apache Groovy, PHP 7+, and C++20.

Simplify three way comparison a b c || b c a || c a b;

You have 3 different boolean comparisons out of which you want 2 to hold. (Strictly, 2 or more, but in your case you can never have all 3). So you can write

a < b && b < c || b < c && c < a || c < a && a < b

as

(a < b) + (b < c) + (c < a) == 2

What is = (the 'Spaceship' Operator) in PHP 7?

The <=> ("Spaceship") operator will offer combined comparison in that it will :

Return 0 if values on either side are equal
Return 1 if the value on the left is greater
Return -1 if the value on the right is greater

The rules used by the combined comparison operator are the same as the currently used comparison operators by PHP viz. <, <=, ==, >= and >. Those who are from Perl or Ruby programming background may already be familiar with this new operator proposed for PHP7.

   //Comparing Integers

echo 1 <=> 1; //output 0
echo 3 <=> 4; //output -1
echo 4 <=> 3; //output 1

//String Comparison

echo "x" <=> "x"; //output 0
echo "x" <=> "y"; //output -1
echo "y" <=> "x"; //output 1


Related Topics



Leave a reply



Submit