Hello, and welcome back! Our journey today takes us into the sorting universe in Ruby. We will learn and utilize Ruby's powerful sorting methods: sort
and sort_by
. These tools in Ruby significantly simplify the task of sorting. Let's get started!
Sorting refers to arranging data in a specific order, which enhances the efficiency of search or merge operations on data. In real life, we sort books alphabetically or clothes by size. Similar concepts are applicable in programming, where sorting large lists of data for more effective analysis is a frequent practice.
Ruby offers built-in sorting methods: sort
for arrays and other enumerables, and sort_by
for more complex sorting logic. Here's a demonstration of how we use these methods:
Sorting with sort
makes sorting arrays of primitives a breeze. Let's see it in action!
Sorting Arrays of Primitives
Sorting Lists of Objects
As you can see, sorting in Ruby is as simple as that!
Ruby allows us to define custom sorting logic using blocks. Let's sort a list of students by their grades, with alphabetical sorting applied in the event of ties in grades. First, let's define the Student
class:
Here's how we perform custom sorting using Ruby's block syntax:
In the example above, we create an array of Student
objects and sort it using a custom comparison defined via a block. The block first compares Student
objects based on their grades in descending order and, in the event of a tie, compares their names in alphabetical order. The <=>
operator returns an integer that indicates the relative order of the objects being compared.
To sort students by grades in ascending order but names in descending order in case of ties, you can adjust the block by flipping the order of the comparisons:
Here, the custom comparison sorts the grades in ascending order while sorting names in descending order when the grades are the same.
While sort
with a block can use custom sorting logic, sort_by
can optimize performance particularly on large datasets by computing the sorting criterion once per element. Here's an example of sorting students first by grade and then name using sort_by
:
sort_by
first evaluates the block for each element in the array, which results in an array of key-object pairs. The array is then sorted, and finally, the original collection is reordered based on this. This can provide a performance boost because it replaces multiple evaluations of a sorting criterion with a single evaluation.
Well done! You've learned how Ruby's sorting functions work and you have utilized Ruby's powerful sorting methods.
In our future lessons, we'll delve deeper into sorting and tackle more intricate problems. So, stay tuned and get ready to sort your way to success! Happy coding!
