bootrails.com is now moving to saaslit.com  See you there!
bootrails moves to  saaslit.com
Post

Sort a Ruby array

Nature of arrays

As we have seen in other blog entries, arrays and hashes are basic concepts in any programming language. It is the best way to encapsulate and organize data. Arrays, specifically, are containers of a list of elements that can be of any nature (strings, arrays, hashes, integers, booleans, etc.).

1
my_array = ['a', false, 'z', 'l', 0, -99, 9.8, nil]

Sorting arrays: the Ruby case

Having cleared the concept of arrays, one may suppose how important it can be to interact with them and organize them according to one’s needs. Ruby has a special method for this purpose, which is .sort. As well, as a “child method” called .sort_by that is used to customize the sorting strategy.

Method: sort

The sort method in ruby sorts any array by comparing each element to another and following ascending logic. In other words, from a to z and -999 to 999.

1
2
3
4
5
6
7
8
9
10
11
my_string_array = ['a', 'z', 'l']
p my_string_array
p my_string_array.sort
# => ["a", "z", "l"]
# => ["a", "l", "z"]

my_number_array = [9, 87, -100, 0]
p my_number_array
p my_number_array.sort
# => [9, 87, -100, 0]
# => [-100, 0, 9, 87]

Note that you can not sort arrays containing elements of different nature, such as strings and integers.

sort!

The difference between .sort and .sort! is that the first one returns “a copy” of the original array sorted, while the second method modifies the order of the original array following the sorted logic. For example:

1
2
3
4
5
6
7
8
9
10
11
12
original_array = ["e", "b", "c", "a", "d"]

new_array = original_array.sort
p new_array
# => ["a", "b", "c", "d", "e"]

p original_array
# => ["e", "b", "c", "a", "d"]

original_array.sort!
p original_array
# => ["a", "b", "c", "d", "e"]

Method: sort_by

This method allows you to sort the data of an array according to specific requirements. We have seen similar behaviour in the group_by method article. The condition to sort the array is passed as a block to the method as per below:

1
2
3
4
5
6
7
fruits = ["strawberry", "banana", "orange", "apple", "melon"]
p fruits.sort
# => ["apple", "banana", "melon", "orange", "strawberry"]

fruits_sorted_by_last_letter = fruits.sort_by { |word| word[-1] }
p fruits_sorted_by_last_letter
# => ["banana", "orange", "apple", "melon", "strawberry"]

Conclusion

We have gone through the most common and easy ways of filtering arrays. However, it is also possible to build the sorting methods by ourselves when we need to customize the filtering. This is going to make our code more complex and might trigger mistakes. However, if you need such customization or if you want to understand the logic behind the built-in methods, there are a lot of examples out there!

Being able to filter arrays is very useful, especially if we want to organize the data of an application so that users can have optimal interaction.

This post is licensed under CC BY 4.0 by the author.