Ruby sort array method
In ruby programming sort() method of array are used to sort an array in ascending order. Generally this method does not directly modify the actual array. Rather returning a new resulting array which contains sorted elements. Syntax of this method as follows.
Syntax | Array.sort(), Array.sort{} |
Parameter | None |
Return | New sorted array |
There are two other forms of this method (Array.sort !, Array.sort_by!). Examples are given in the last section.
Ruby sort() method example
This method are used in two ways, First is simple which is not take any parameter. And other are comparison based sorting. let's takes few examples to understand this method functionality.
# Example 1
# Our array
arr = [2, 6, -4, 1, 3, 4]
# Sort operation
result = arr.sort()
# arr element
print(" arr : ",arr)
# sort element
print("\n result : ",result)

arr : [2, 6, -4, 1, 3, 4]
result : [-4, 1, 2, 3, 4, 6]
The example above is based on a simple approach that sort the elements of an integer array. See the other example which is based on comparison.
# Example 2
# Array element
record = [5,6,2,3,9,24,1,3]
# Simple sort
a = record.sort
# Comparison sort
b = record.sort {|x, y| y <=> x}
# Display value
print("record : ",record)
print("\na : ",a)
print("\nb : ",b)

record : [5, 6, 2, 3, 9, 24, 1, 3]
a : [1, 2, 3, 3, 5, 6, 9, 24]
b : [24, 9, 6, 5, 3, 3, 2, 1]
This method are work on similar type of objects. If the array is a form of an element collection, the resulting array do not modify the inner element. Only its order will change. let's see an example.
# Example 3
# Array type array elements
num = [[4,9],[2,7],[4,4],[-2,0],[4,7]]
word = ["php","c","ruby","node"]
# Sort operation
a = num.sort()
b = word.sort()
# Display value
print(" num : ",num);
print("\n a : ",a);
print("\n\n word : ",word);
print("\n b : ",b);

num : [[4, 9], [2, 7], [4, 4], [-2, 0], [4, 7]]
a : [[-2, 0], [2, 7], [4, 4], [4, 7], [4, 9]]
word : ["php", "c", "ruby", "node"]
b : ["c", "node", "php", "ruby"]
Ruby sort method variation
Array.sort! and Array.sort_by! is other two method which is used in ruby programming. In this section we will understand examples of their functionality in how it works.
Array.sort! method are modifies actual array in place. for example.
# Example Array.sort!
# Our array
arr = [2, 6, -4, 1, 3, 4]
# Before sort array element
print(" Before sort : ",arr)
# Sort operation
arr.sort!
# After sort array element
print("\n After sort : ",arr)

Before sort : [2, 6, -4, 1, 3, 4]
After sort : [-4, 1, 2, 3, 4, 6]
Array.sort_by! this method are sort the array elements by using expression.
# Example Array.sort_by!
# Our array
record = [[4,5,9],[2,1,7],[4,8,4],[-2,1,0],[4,5,7]]
# Before sort array element
print(" Before sort : ",record)
# Sort elements of array by inner second element
record.sort_by!{ |record| record[1] }
# After sort array element
print("\n After sort : ",record)

Before sort : [[4, 5, 9], [2, 1, 7], [4, 8, 4], [-2, 1, 0], [4, 5, 7]]
After sort : [[2, 1, 7], [-2, 1, 0], [4, 5, 9], [4, 5, 7], [4, 8, 4]]
In the above example, the array is manipulated using the second element of the internal array.
Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.
New Comment