#Sorting in Ruby

Sam Jomaiv
3 min readAug 13, 2021

Sorting in Ruby isn't too complicated as you might think, but there are different ways of doing it. In this blog I will talk about sorting in Ruby. But before let me explain what sorting is why is it important.

In computer science, a sorting algorithm is an algorithm that puts elements of a list into an order. The most frequently used orders are numerical order and lexicographical order, and either ascending or descending. Efficient sorting is important for optimizing the efficiency of other algorithms (such as search and merge algorithms) that require input data to be in sorted lists.

Sorting is an important aspect of any business that affects sales. A poor sorting functionality on a website could lead to losses for a particular business. If customers of a website can’t easily find what they are looking for (for example if a customer cannot view products by category or price) that could drive people off your platform. So now that we know how sorting is important, let’s take a look under the hood how these sorting functionalities work, particularly in Ruby.

Ruby’s #sort method can be called a couple different ways. If you have a simple array of strings or numbers, for example, you can call #sort without a block, and Ruby will do its best to sort the elements:

nums = [1, 5, 3]
sorted_nums = nums.sort
sorted_nums
# => [1, 3, 5]

Note that in some languages like JavaScript, #sort will alter the original array. But in Ruby #sort does not alter the original array.

When sorting more complex data, you can also provide a block and write a sorting algorithm of your own. The block must return either -1, 0, or 1 to determine the sort order. This can result in some not-so-pleasant code:

users = [
{ name: "Liza", phone: "555-555-5556" },
{ name: "Duane", phone: "555-555-5555"},
{ name: "Cara", phone: "555-555-5556"},
]
sorted_users = users.sort do |user1, user2|
if user1[:name] == user2[:name]
0
elsif user1[:name] < user2[:name]
-1
elsif user1[:name] > user2[:name]
1
end
end
sorted_users
# => [{:name=>"Cara", :phone=>"555-555-5556"}, {:name=>"Duane", :phone=>"555-555-5555"}, {:name=>...

We can simplify the code above a bit by using the “spaceship operator” (<=>). The spaceship operator, also called the combined comparison operator, returns:

  • 0 if the first operand equals the second,
  • -1 if the first operand is less than the second, and
  • 1 if the first operand is greater than the second.

So, instead of utilizing if and elsif logic like we did above, we can simply call #sort with the following code:

users.sort do |user1, user2|
user1[:name] <=> user2[:name]
end
# => [{:name=>"Cara", :phone=>"555-555-5556"}, {:name=>"Duane", :phone=>"555-555-5555"}, {:name=>...

Ruby also provides a #sort_by method, which, instead of passing two elements to the block and requiring you to write the comparison logic, just passes one element to the block:

users.sort_by do |user1|
user1[:name]
end
# => [{:name=>"Cara", :phone=>"555-555-5556"}, {:name=>"Duane", :phone=>"555-555-5555"}, {:name=>...

So now I hope that you have some idea on how sorting functionalities work in Ruby, as you see in examples above, Ruby makes sorting enjoyable, unlike JavaScript, the code looks a lot cleaner and more understandable which makes our life a lot easier.

--

--