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 not create any 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 -> 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: ["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 method. The Array instance which is being returned from the method is in the ascending order. This method is not creating any impact on the original Array instance because this method is one of the examples of non-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: ["Subha", "Sham", "Raat", "Vivek", "Me", "Amisha", "Zain", "Harsh", "Bajwa"]

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 a 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 not creating any impact on the original Array instance because this method is one of the examples of non-destructive methods.




Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.