[英]What's the difference between <=> and - in a block for sort method?
I have an array like this: 我有一个像这样的数组:
my_array = [1,3,5,5,6,7]
to be sorted from the biggest to the smallest. 从最大到最小排序。 I do these:
我做这些:
my_array.sort { |a,b| b <=> a }
my_array.sort { |a,b| b - a }
Both return the same thing: [7,6,5,5,3,1]
. 两者都返回相同的东西:
[7,6,5,5,3,1]
。 Can someone explain what the difference is between those two? 有人可以解释一下两者之间的区别吗?
sort → array sort { |a, b|
排序→数组排序{| a,b | block } → array Returns an array containing the items in enum sorted, either according to their own <=> method, or by using the results of the supplied block.
block}→array返回一个数组,该数组包含按枚举排序的项目,这些项目是根据其自己的<=>方法,还是通过使用提供的块的结果。 The block should return -1, 0, or +1 depending on the comparison between a and b.
该块应返回-1、0或+1,具体取决于a和b之间的比较。 As of Ruby 1.8, the method Enumerable#sort_by implements a built-in Schwartzian Transform, useful when key computation or comparison is expensive.
从Ruby 1.8开始,方法Enumerable#sort_by实现了内置的Schwartzian转换,在密钥计算或比较昂贵时很有用。
So there is no official mentioning when the evaluated value of the block is other than -1
, 0
, or 1
. 所以没有官方提时该块的评价值比其它
-1
, 0
,或1
。 If what you did works, then it is perhaps working under the principle that negative numbers are regarded as -1
and positive numbers are regarded as 1
. 如果您所做的工作有效,则可能是在将负数视为
-1
和将正数视为1
的原理下工作。 If that is the case, there is no difference. 如果是这样,没有区别。 However, since it is not officially stated so, I think it is safer to use
{|a, b| b <=> a}
但是,由于尚未正式声明,因此我认为使用
{|a, b| b <=> a}
更安全{|a, b| b <=> a}
{|a, b| b <=> a}
One subtracts, one uses the items' <=>
operator. 一个减去,一个使用项目的
<=>
运算符。
You're using numbers, Fixnum
to be precise, so subtracting produces < 0
, 0
, or > 0
. 您使用的数字,
Fixnum
要精确,所以减法产生< 0
0
,或> 0
。
<=>
for Fixnum
calls the C routine: <=>
对于Fixnum
调用C例程:
if (x == y) return INT2FIX(0);
if (FIXNUM_P(y)) {
if (FIX2LONG(x) > FIX2LONG(y)) return INT2FIX(1);
return INT2FIX(-1);
}
which returns -1, 0, or 1, which produces the same results–because the sort only checks the sign. 它返回-1、0或1,它会产生相同的结果-因为排序仅检查符号。
The bottom line is that in this case there's no difference, but that's an implementation detail. 底线是在这种情况下没有区别,但这是实现细节。
It's a kind of trick. 这是一种把戏。 The block used in sort an array must return a comparaison result, -1 (a lower than b), 0 (a equal b) , 1 ( a higher than b).
用于对数组进行排序的块必须返回比较结果-1(小于b的a),0(等于b的a),1(大于b的a)。
What happens, is that code seems to use the sign of the operation only, and not the value so (ba) operation return the same sign than b <=> a operation: 发生的是,代码似乎仅使用操作的符号,而不使用值,因此(ba)操作返回的符号与b <=>操作相同:
1-10 -9 # neg
1<=>10 -1 # neg
5-5 0 # eq
5<=>5 0 # eq
10-1 9 # pos
10<=>1 1 # pos
But I strongly suggest to use the comparaison operator instead of the difference. 但是我强烈建议使用比较运算符而不是差异。
It's important to understand what <=>
is doing, as it's the crux for sorting and comparisons: 了解
<=>
在做什么很重要,因为这是排序和比较的关键:
The <=>
operator (AKA "spaceship") tells us whether one value is less-than, equal, or greater-than another, which makes it easy to know when code should swap them. <=>
运算符(又称“太空飞船”)告诉我们一个值是小于,等于还是大于另一个值,这使您很容易知道何时应该交换它们。 For instance: 例如:
0 <=> 1 # => -1
1 <=> 1 # => 0
2 <=> 1 # => 1
In other words, -1
means the comparison was less-than, 0
was equal, and 1
was greater-than. 换句话说,
-1
表示比较小于, 0
等于, 1
大于。 Knowing that, code can swap the values one of two ways or ignore swapping. 知道这一点,代码可以用两种方法之一交换值或忽略交换。
Using -
works for numbers, but it's going to have really unexpected results with other data-types, like strings, arrays, hashes, which use -
differently, if at all. 使用
-
适用于数字,但与其他数据类型(例如字符串,数组,哈希)的使用却会产生出乎意料的结果,如果使用-
不同。
Read the documentation for the Comparable module, which makes it really easy to add additional functionally to a class by defining the <=>(other)
method and to understand what you'll gain by using it. 阅读Comparable模块的文档,该文档通过定义
<=>(other)
方法,使您很容易在类中添加其他功能,并理解使用该模块会获得什么。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.