怎么排序!红宝石中的数组

时间:2021-02-04 15:56:28

I want to sort my_array and then reverse the order.

我想对my_array进行排序,然后颠倒顺序。

Which markup is correct?

哪个标记是正确的?

my_array.sort.reverse!

or

要么

my_array.sort!.reverse

Or does it make any difference?

或者它有什么不同?

Thanks

谢谢

4 个解决方案

#1


2  

You have to decompose the chain :

你必须分解链:

First, let's understand the difference between the sort and the sort! method. If I write

首先,让我们理解排序和排序之间的区别!方法。如果我写

array = [7,2,4]
array.sort!
array # => [2,4,7]

If you write

如果你写

array = [7,2,4]
foo = array.sort
array # => [7,2,4]
foo # => [2,4,7]

The sort method sort the array and returns the result as the output of the function, whereas the sort! one directly modifies the existing array.

sort方法对数组进行排序,并将结果作为函数的输出返回,而排序!一个直接修改现有数组。

So if you write :

所以,如果你写:

my_array.sort.reverse!

It is like writing :

就像写作:

(my_array.sort). # => Here we create a new array who is calculated by sorting the existing one
reverse! # => Then we reverse this new array, who is not referenced by a variable.

If you write :

如果你写:

(my_array.sort!). #=> Here you sort my_array and reinject the result into my_array !
reverse  # Then reverse it and inject the result into a NEW array

So in both cases, you will not obtain what you want ! What you want to do is either :

所以在这两种情况下,你都无法获得你想要的东西!你想要做的是:

my_array.sort!.reverse!

or :

要么 :

new_array = my_array.sort.reverse

#2


1  

You'll get the same output, but one will modify the initial array. See this:

您将获得相同的输出,但会修改初始数组。看到这个:

2.1.1 :001 > my_array = ["a","d","b","c"]
 => ["a", "d", "b", "c"] 

Just declaring an array with a, b, c, and d in completely wrong orders.

只需在完全错误的订单中声明带有a,b,c和d的数组。

2.1.1 :002 > my_array.sort.reverse!
 => ["d", "c", "b", "a"] 

Running your first command on it returns a reverse-sorted array

在它上面运行第一个命令会返回一个反向排序的数组

2.1.1 :003 > my_array
 => ["a", "d", "b", "c"] 

... but doesn't modify the original array itself.

...但不修改原始数组本身。

2.1.1 :004 > my_array.sort!.reverse
 => ["d", "c", "b", "a"] 

Running the second command returns the same result, the array sorted backwards

运行第二个命令会返回相同的结果,该数组会向后排序

2.1.1 :005 > my_array
 => ["a", "b", "c", "d"] 

But the array itself has been modified, but only by the sort! call. A ! after a method call 'saves' the changes to the object it's called on and returns the result. So in the second one:

但阵列本身已经被修改,但只是排序!呼叫。一个 !在方法调用'保存'后,对其调用的对象进行更改并返回结果。所以在第二个:

  • You sort the array, saving the changes to it and returning the sorted array, then
  • 您对数组进行排序,保存对它的更改并返回已排序的数组
  • Run reverse on the sorted array, which doesn't save to anything and only returns the result.
  • 在已排序的数组上运行reverse,它不保存到任何内容,只返回结果。

#3


0  

my_array.sort!.reverse will modify the receiver so my_array will be sorted after this call e.g.

my_array.sort!.reverse将修改接收器,以便在此调用之后对my_array进行排序,例如

my_array = [1,4,3,5,2]
my_array.sort!.reverse
#=> [5,4,3,2,1]
my_array 
#=> [1,2,3,4,5]

the second form my_array.sort.reverse! will not modify my_array because sort will dup the array first then reverse! will modify this duped copy which is not being stored. This would have the same impact as my_array.sort.reverse without the !

第二种形式my_array.sort.reverse!不会修改my_array,因为sort会首先复制数组然后反转!将修改未存储的这个欺骗副本。这与my_array.sort.reverse具有相同的影响而没有!

my_array = [1,4,3,5,2]
my_array.sort.reverse!
#=> [5,4,3,2,1]
my_array 
#=> [1,4,3,5,2]

Thirdly, something like my_array.sort!.reverse! will modify the receiver twice meaning my_array.sort! will sort the array in place and then .reverse! will reverse the array in place. so my_array will now be sorted and reversed.

第三,像my_array.sort!.reverse!将修改接收器两次意味着my_array.sort!将数组排序到位,然后.reverse!将阵列反转到位。所以my_array现在将被排序和反转。

my_array = [1,4,3,5,2]
my_array.sort!.reverse!
#=> [5,4,3,2,1]
my_array 
#=> [5,4,3,2,1]

Although in this case I do not think you will need either bang ! method as the second form has no impact. bang ! in ruby means "dangerous", may alter the data or have other unexpected results.

虽然在这种情况下,我认为你不会需要爆炸!方法作为第二种形式没有影响。砰!在ruby中意为“危险”,可能会改变数据或产生其他意外结果。

#4


0  

 my_array.sort!.reverse! 

is the correct answer, since you want to change the array you already have.

是正确答案,因为您想要更改已有的阵列。

 my_array.sort.reverse! 

creates a sorted copy and reverses it (the original doesn't change).

创建一个已排序的副本并将其反转(原始副本不会更改)。

 my_array.sort!.reverse 

sorts the original and creates a reversed copy (the original isn't reversed).

对原稿进行排序并创建反转副本(原件不会反转)。

#1


2  

You have to decompose the chain :

你必须分解链:

First, let's understand the difference between the sort and the sort! method. If I write

首先,让我们理解排序和排序之间的区别!方法。如果我写

array = [7,2,4]
array.sort!
array # => [2,4,7]

If you write

如果你写

array = [7,2,4]
foo = array.sort
array # => [7,2,4]
foo # => [2,4,7]

The sort method sort the array and returns the result as the output of the function, whereas the sort! one directly modifies the existing array.

sort方法对数组进行排序,并将结果作为函数的输出返回,而排序!一个直接修改现有数组。

So if you write :

所以,如果你写:

my_array.sort.reverse!

It is like writing :

就像写作:

(my_array.sort). # => Here we create a new array who is calculated by sorting the existing one
reverse! # => Then we reverse this new array, who is not referenced by a variable.

If you write :

如果你写:

(my_array.sort!). #=> Here you sort my_array and reinject the result into my_array !
reverse  # Then reverse it and inject the result into a NEW array

So in both cases, you will not obtain what you want ! What you want to do is either :

所以在这两种情况下,你都无法获得你想要的东西!你想要做的是:

my_array.sort!.reverse!

or :

要么 :

new_array = my_array.sort.reverse

#2


1  

You'll get the same output, but one will modify the initial array. See this:

您将获得相同的输出,但会修改初始数组。看到这个:

2.1.1 :001 > my_array = ["a","d","b","c"]
 => ["a", "d", "b", "c"] 

Just declaring an array with a, b, c, and d in completely wrong orders.

只需在完全错误的订单中声明带有a,b,c和d的数组。

2.1.1 :002 > my_array.sort.reverse!
 => ["d", "c", "b", "a"] 

Running your first command on it returns a reverse-sorted array

在它上面运行第一个命令会返回一个反向排序的数组

2.1.1 :003 > my_array
 => ["a", "d", "b", "c"] 

... but doesn't modify the original array itself.

...但不修改原始数组本身。

2.1.1 :004 > my_array.sort!.reverse
 => ["d", "c", "b", "a"] 

Running the second command returns the same result, the array sorted backwards

运行第二个命令会返回相同的结果,该数组会向后排序

2.1.1 :005 > my_array
 => ["a", "b", "c", "d"] 

But the array itself has been modified, but only by the sort! call. A ! after a method call 'saves' the changes to the object it's called on and returns the result. So in the second one:

但阵列本身已经被修改,但只是排序!呼叫。一个 !在方法调用'保存'后,对其调用的对象进行更改并返回结果。所以在第二个:

  • You sort the array, saving the changes to it and returning the sorted array, then
  • 您对数组进行排序,保存对它的更改并返回已排序的数组
  • Run reverse on the sorted array, which doesn't save to anything and only returns the result.
  • 在已排序的数组上运行reverse,它不保存到任何内容,只返回结果。

#3


0  

my_array.sort!.reverse will modify the receiver so my_array will be sorted after this call e.g.

my_array.sort!.reverse将修改接收器,以便在此调用之后对my_array进行排序,例如

my_array = [1,4,3,5,2]
my_array.sort!.reverse
#=> [5,4,3,2,1]
my_array 
#=> [1,2,3,4,5]

the second form my_array.sort.reverse! will not modify my_array because sort will dup the array first then reverse! will modify this duped copy which is not being stored. This would have the same impact as my_array.sort.reverse without the !

第二种形式my_array.sort.reverse!不会修改my_array,因为sort会首先复制数组然后反转!将修改未存储的这个欺骗副本。这与my_array.sort.reverse具有相同的影响而没有!

my_array = [1,4,3,5,2]
my_array.sort.reverse!
#=> [5,4,3,2,1]
my_array 
#=> [1,4,3,5,2]

Thirdly, something like my_array.sort!.reverse! will modify the receiver twice meaning my_array.sort! will sort the array in place and then .reverse! will reverse the array in place. so my_array will now be sorted and reversed.

第三,像my_array.sort!.reverse!将修改接收器两次意味着my_array.sort!将数组排序到位,然后.reverse!将阵列反转到位。所以my_array现在将被排序和反转。

my_array = [1,4,3,5,2]
my_array.sort!.reverse!
#=> [5,4,3,2,1]
my_array 
#=> [5,4,3,2,1]

Although in this case I do not think you will need either bang ! method as the second form has no impact. bang ! in ruby means "dangerous", may alter the data or have other unexpected results.

虽然在这种情况下,我认为你不会需要爆炸!方法作为第二种形式没有影响。砰!在ruby中意为“危险”,可能会改变数据或产生其他意外结果。

#4


0  

 my_array.sort!.reverse! 

is the correct answer, since you want to change the array you already have.

是正确答案,因为您想要更改已有的阵列。

 my_array.sort.reverse! 

creates a sorted copy and reverses it (the original doesn't change).

创建一个已排序的副本并将其反转(原始副本不会更改)。

 my_array.sort!.reverse 

sorts the original and creates a reversed copy (the original isn't reversed).

对原稿进行排序并创建反转副本(原件不会反转)。