Custom Sorting in Ruby

We can sort the array of integers in ruby easily, just by using the sort function. But what if we want to sort array of arrays or array of objects etc. For this we need some thing where we can provide our sorting conditions. Ruby provide this functionality using sort and sort_by functions.

Lets take a example of array of arrays:

arr = [[1,4,3],[2,1,7],[3,2,8]]

Using sort:

Now you want to sort this array in the ascending order of the middle element of the inner arrays. All you need to do is:

arr.sort! do |x,y| 
	#if this result is 1 means x should come later relative to y
	#if this result is -1 means x should come earlier relative to y
	#if this result is 0 means both are same so position doesn't matter
	x[1] <=> y[1] #this return 0 or -1 or 1
end

Output : [[2, 1, 7], [3, 2, 8], [1, 4, 3]] 

Here x,y are the elements of arrays like ‘[1,4,3]’ (a element of array) and using x[1] and y[1] we are comparing the middle elements. Now lets look at opeartor <=> :

2 <=> 5 # return -1 because 2 is smaller than 5
5 <=> 2 # return 1 because 5 is larger than 2
2 <=> 2 # return 0

Now if you don’t want to use <=> operator then you can do this like:

arr.sort! do |x,y|
	if x[1] > y[1]
		1
	elsif x[1] < y[1]
	   -1
	else
	  0
	end
end

#-------------or----------------

arr.sort! do |x,y|
	x[1] - y[1]
end

Using sort_by

Here you basically focus on the part of element using which you want to sort the whole array.

arr.sort_by!{|x| x[1]}
comments powered by Disqus