Home » Ruby programming

Array.sort_by Method with Example in Ruby

Ruby Array.sort_by Method: Here, we are going to learn about the Array.sort_by Method with examples in Ruby programming language.
Submitted by Hrithik Chandra Prasad, on February 12, 2020

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.

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.

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:

=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.

Example 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.







Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.