ruby array_Array.sort! Ruby中的示例方法

ruby array

Array.sort! 方法 (Array.sort! Method)

In this article, we will study about Array.sort! 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! 方法 。 你们都必须认为该方法必须执行与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 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方法对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"]


翻译自: https://www.includehelp.com/ruby/array-sort-inverse-method-with-example.aspx

ruby array

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值