Home » Ruby programming

Array.sort! Method with Example in Ruby

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

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.

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.

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:

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

Example 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"]





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.