ruby sort_by_Ruby中带有示例的Array.sort_by方法

ruby sort_by

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. This method requires certain parameters on which basis the sorting is going to happen. You have to state that criteria inside the block. If you are invoking this method without the block then an enumerator will be returned by the method. This method will not create an impact on the actual arrangement of objects in the self Array because this method belongs to the category of non-destructive methods where the changes created by the method are temporary or non-permanent.

该方法是一个公共实例方法,为Ruby库中的Array类定义。 此方法的工作方式是,在对使用该方法调用的Array进行排序之后,它返回一个新的Array。 此方法需要某些参数,在此基础上将要进行排序。 您必须在块内声明该条件。 如果在不使用该块的情况下调用此方法,则该方法将返回一个枚举数。 此方法不会对self Array中对象的实际排列产生影响,因为该方法属于非破坏性方法的类别,在该类别中,该方法创建的更改是临时的或非永久的。

Syntax:

句法:

    array_instance.sort_by -> Enumerator
    or
    array_instance.sort_by{|object| 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 on the basis of certain criteria.

此方法不带任何参数。 相反,如果您要根据某些条件进行排序,则必须传递一个块。

Example 1:

范例1:

=begin
  Ruby program to demonstrate sort_by method
=end

# array declaration
table = ["Subha","Sham","Raat","Vivek","Me","Amisha","Zain","Harsh","Bajwa"]

puts "Array sort_by implementation"
new_arr = table.sort_by{|string| string.size}

puts "Array after sorting: #{new_arr}"
puts "Original Array instance: #{table}"

Output

输出量

Array sort_by implementation
Array after sorting: ["Me", "Sham", "Raat", "Zain", "Subha", "Vivek", "Harsh", "Bajwa", "Amisha"]
Original Array instance: ["Subha", "Sham", "Raat", "Vivek", "Me", "Amisha", "Zain", "Harsh", "Bajwa"]

Explanation:

说明:

In the above code, you can observe that we are sorting the Array instance with the help of the Array.sort_by method. We are sorting the Array object based on its length or size. You can see that the object "me" has the smallest length that is why it is the first element of the new Array. This method is not creating an impact upon the original Array instance because this method is one of the examples of non-destructive methods.

在上面的代码中,您可以观察到我们正在使用Array.sort_by方法对Array实例进行排序。 我们正在根据其长度或大小对Array对象进行排序。 您会看到对象“ me”具有最小的长度,这就是为什么它是新Array的第一个元素。 此方法不会对原始Array实例产生影响,因为该方法是非破坏性方法的示例之一。

Example 2:

范例2:

=begin
  Ruby program to demonstrate sort_by method
=end

# array declaration
table = ["Subha","Sham","Raat","Vivek","Me","Amisha","Zain","Harsh","Bajwa"]

puts "Array sort_by implementation"
new_arr = table.sort_by

puts "Array after sorting: #{new_arr}"
puts "Original Array instance: #{table}"

Output

输出量

Array sort_by implementation
Array after sorting: #<Enumerator:0x000055dabc680870>
Original Array instance: ["Subha", "Sham", "Raat", "Vivek", "Me", "Amisha", "Zain", "Harsh", "Bajwa"]

Explanation:

说明:

In the above output, you can notice that the method is returning an enumerator when it is called without the block.

在上面的输出中,您会注意到,在没有该块的情况下调用该方法 ,该方法将返回一个枚举数。

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

ruby sort_by

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值