FAQ: Methods, Blocks, & Sorting - Getting Technical

I am also confused on what is happening with the sorting operation with more than 2 book titles. I understand that it is only comparing two objects at a time, iterating through the book titles and comparing firstBook and secondBook. But if the result of those comparisons can only be -1, 1, or 0, how does it know the difference between two comparisons that both result in -1 (or both result in 1 or both result in 0)?

Is it doing something like this for Books A, B, C and D assuming each is titled “A”, “B”, “C”, and “D”, respectively?:

Book A <=> Book A = 0
Book A <=> Book B = -1
Book A <=> Book C = -1
Book A <=> Book D = -1

Book B <=> Book A = 1
Book B <=> Book B = 0
Book B <=> Book C = -1
Book B <=> Book D = -1

Book C <=> Book A = 1
Book C <=> Book B = 1
Book C <=> Book C = 0
Book C <=> Book D = -1

Book D <=> Book A = 1
Book D <=> Book B = 1
Book D <=> Book C = 1
Book D <=> Book D = 0

And now it knows that, for example, B is greater than A but less than C and less than D, so it knows to put B after A and before C? Am I thinking about this process correctly?

1 Like

When comparisons match (meaning 0), no swap takes place. The sign on the one indicates the direction the A will move in the swap, if I’m not mistaken.

Under the hood, Ruby is likely using a very efficient sorting method (something to research along with the various methods, themselves. Efficient sorting is not just about how efficient the algorithm is, but how much time it takes. Some methods are faster on smaller data sets, and others slower, even while they are immensely faster on larger data sets. Each have advantages and disadvantages. At our stage in learning, none of the above matters, but it will once you get into a production environment and do this for a living.


For more about the sort method, here is a useful and detailed explanation by Tony DiNitto.

1 Like

Great question, I was wondering too! Let’s keep moving forward, and we will know what’s going on when the time is right.

dafuq is a bang operator?


1 Like

Is there any good reason why this tutorial is using camelCase, after telling learners in the introduction that the Ruby naming convention is snake_case? Just seems like a bad choice that is going to confuse people.

If it’s any consolation, I’ve never participated in a coding course that didn’t urge learners to stick to naming conventions and then go right ahead and ignore its own advice.

1 Like

Hi everyone!

I’m repeating a question that was asked above because no one has explained it properly.

What is the point of the fancy block of code on line 4 when we can just use this:



What am I missing?


You are not missing anything. It should still bear out that the demonstrated method is valid, and a good example of use case with block parameters. True competence is not just about knowing the simplest or easiest syntax, but knowing all available methods and how they each work. Take in everything regardless how trivial or naïve, and avoid cherry-picking in these learning stages.


my approach is slightly different than what people have mentioned here, it gets the desired answer but I’m not if the answer is logical. Your input is appreciated, thanks!

#my code is as follows
books = [“Charlie and the Chocolate Factory”, “War and Peace”, “Utopia”, “A Brief History of Time”, “A Wrinkle in Time”]
books.sort! { |firstBook, secondBook| firstBook <=> secondBook }
books.sort! { |firstBook, secondBook| secondBook <=> firstBook }
puts books

What happens if you remove that line?

Then nothing happens, because the sort! will change it in place right? I left it there as part of the instruction.

Okay, found the lesson. Line 4 is meant as an example to follow, so can be commented out.

Yes, the bang! operator is what makes that so.

The combined comparison operator passes the result (-1, 0, or 1) to the sort so it can perform a swap, if needed. When sorting in descending order the swap occurs when that result is negative one (-1). We don’t have any hand with the logic in the background. Ultimately there is nothing amiss in the logic above.


I guess i didn’t frame my question the way I wanted to- does it matter if I swap the values between the spaceship operator, or the values in the block parameter?

1 Like

It shouldn’t matter whether the parameters are reordered or the operands, though for consistency I would leave the parameters ordered as A and B, and switch the order of the operands.

books.sort! { |A, B| B <=> A }
1 Like

great thanks! also for the fast reply. I’m enjoying studying the course so far.

1 Like

I’ve found that the ruby course on codecademy is somewhat frustrating…