ruby array
Array.sort_by! 方法 (Array.sort_by! Method)
In this article, we will study about Array.sort_by! Method. You all must be thinking the method must be doing something related to the sorting of elements or objects in the Array instance. It is not as simple as it looks. Well, we will figure this out in the rest of our content. We will try to understand it with the help of syntax and demonstrating program codes.
在本文中,我们将研究Array.sort_by! 方法 。 你们都必须认为该方法必须执行与Array实例中的元素或对象的排序有关的操作。 它并不像看起来那么简单。 好吧,我们将在其余内容中解决这个问题。 我们将尝试借助语法并演示程序代码来理解它。
Method description:
方法说明:
This method is a public instance method and defined for the Array class in Ruby's library. This method works in a way that it returns a new Array after sorting the Array with which the method has been invoked. If you are invoking the method without the block then the sorting will be done in the ascending order. If you want the sorting to be done in descending order then you are supposed to pass a block along with the method at the time of its invocation. This method will create an impact on the actual arrangement of objects in the self Array because this method belongs to the category of destructive methods where the changes created by the method are non-temporary or permanent.
该方法是一个公共实例方法,为Ruby库中的Array类定义。 此方法的工作方式是,在对使用该方法调用的Array进行排序之后,它返回一个新的Array。 如果您在不带块的情况下调用方法,则排序将以升序进行。 如果希望以降序进行排序,则应该在调用该方法时将一个块与该方法一起传递。 此方法将对self Array中对象的实际排列产生影响,因为该方法属于破坏性方法的类别,在该类别中,该方法创建的更改是非临时的或永久的。
Syntax:
句法:
array_instance.sort! -> new_array
or
array_instance.sort!{|a,b| block}-> new_array
Argument(s) required:
所需参数:
This method does not take any argument. Instead, you will have to pass a block if you want to sort in a different way.
此方法不带任何参数。 相反,如果要以其他方式排序,则必须传递一个块。
Example 1:
范例1:
=begin
Ruby program to demonstrate sort! method
=end
# array declaration
table = ["Subha","Sham","Raat","Vivek","Me","Amisha","Zain","Harsh","Bajwa"]
puts "Array sort implementation"
new_arr = table.sort!
puts "Array after sorting: #{new_arr}"
puts "Original Array instance: #{table}"
Output
输出量
Array sort implementation
Array after sorting: ["Amisha", "Bajwa", "Harsh", "Me", "Raat", "Sham", "Subha", "Vivek", "Zain"]
Original Array instance: ["Amisha", "Bajwa", "Harsh", "Me", "Raat", "Sham", "Subha", "Vivek", "Zain"]
Explanation:
说明:
In the above code, you can observe that we are sorting the Array instance with the help of Array.sort_by! method. The Array instance which is being returned from the method is in the ascending order. This method is creating an impact upon the original Array instance because this method is one of the examples of destructive methods.
在上面的代码中,您可以观察到我们正在借助Array.sort_by对Array实例进行排序! 方法 。 从方法返回的Array实例按升序排列。 此方法对原始Array实例产生影响,因为该方法是破坏性方法的示例之一。
Example 2:
范例2:
=begin
Ruby program to demonstrate sort! method
=end
# array declaration
table = ["Subha","Sham","Raat","Vivek","Me","Amisha","Zain","Harsh","Bajwa"]
puts "Array sort! implementation"
new_arr = table.sort!{|a,b| b<=>a}
puts "Array after sorting: #{new_arr}"
puts "Original Array instance: #{table}"
Output
输出量
Array sort! implementation
Array after sorting: ["Zain", "Vivek", "Subha", "Sham", "Raat", "Me", "Harsh", "Bajwa", "Amisha"]
Original Array instance: ["Zain", "Vivek", "Subha", "Sham", "Raat", "Me", "Harsh", "Bajwa", "Amisha"]
Explanation:
说明:
In the above code, you can observe that we can do descending order sorting as well. This can be done with the help of block and <=> operator. <=> operator will return -1 when a is smaller than b, +1 when a is greater than b and 0 when a and b are equal. This method is creating an impact upon the original Array instance because this method is one of the examples of destructive methods.
在上面的代码中,您可以观察到我们也可以进行降序排序。 这可以借助block和<=>运算符来完成。 <=>运算符将在a小于b时返回-1,在a大于b时返回+1 ,在a和b相等时返回0。 此方法对原始Array实例产生影响,因为该方法是破坏性方法的示例之一。
翻译自: https://www.includehelp.com/ruby/array-sort_by-inverse-method-with-example.aspx
ruby array