FAQ: Methods, Blocks, & Sorting - Getting Technical

This community-built FAQ covers the “Getting Technical” exercise from the lesson “Methods, Blocks, & Sorting”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Learn Ruby

FAQs on the exercise Getting Technical

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

Hi there,

I’m quite lost with the sorting method in descending order.
Can we choose any “name” we want as an argument?

Code is as follow :
books.sort! { |secondBook, firstBook| firstBook <=> secondBook }

Where do firstBook and secondBook come from? And how ruby understands that it has to be sorted from Z to A.

Thanks for answers.

1 Like

I’m not an expert but the comparison operator <=> compares the value from =, > or <… so assuming none of the values are truely equal if you place the secondbook on the left of the comparison and the firstbook on the right it will force the function to list them in reverse order.

The | | brings the values into the method sort… although those details are fuzzy for me too so would be curious to hear more.

1 Like
|secondBook, firstBook|

is referred to as the block parameters. These are local variables used by the block. We can use any names we wish.

Technically, combined comparison operator which is not a boolean operator, as such, but returns a numeric, -1, 0 or 1, depending how the LHS relates to the RHS.

How we order the block variables determines the order of the sort.

1 Like

So the default of sort (in ascending numerical or alphabetical order) will apply and only be changed if overridden by the block given as a parameter?

1 Like

The block parameters are required, and the sort order depends on how they are ordered.

If you have an array of numbers it will sort in ascending order - no parameters given

So I ask because if you add parameters sort {|num1,num2| num2 <=> num1 } - I would assume in order for this to be able to work sort would already have to have the numbers from lowest to highest so that we know num1 <num2 before it is compared in num2 <=> num1 ?
In my mind just to clarify


What you have found is the default. It also disproves what I wrote above, and I stand corrected. From this standpoint it follows that in order to circumvent this, we will need the block params. I hope we’re putting the right edge on this.

1 Like

I am confused, why would you not just use a built-in array option? Am I missing something?

Is there a benefit (time, resource, etc.) associated with the longer code?
Is there a penalty for using the array operators?

Since the previous code sorted the array from A-Z, I used:



Hi all,

I’m pretty confused about hashes and arrays now that we’re sorting them.

Specifically this part:
books.sort! { |firstBook, secondBook| firstBook <=> secondBook }

Does |firstBook, secondBook| assign the books to a key and a value? or is it only comparing two books? I don’t fully understand how the whole list of books is sorted by the first and second book.

We are working specifically with an array, not a hash. The curly braces above are the block delimiters of the sort! method.

object.method { block }

They are known as block parameters, their names are arbitrary and only apply within the method block as iteration variables. They are not keys, since we are not dealing with a hash.

1 Like

Thanks for your reply, after finishing the intro course I guess I need to do some further reading on terminology so I can understand it better.

1 Like

I came to ask exactly this question. Why not just do books.sort! then books.reverse! ?


Anyone else use the “.reverse!” method? I was able to get the correct answer it looks like and with less coding.


The real question is, do we want this object reversed, or do we want a copy, reversed? The bang operator is in-place so the original object is reversed.

1 Like

I think the confusing thing here (for me anyway) is why pass block parameters "|secondBook, firstBook| " when you can just use books.sort.reverse and get the same answer?

The answer appears to be that for a simple array of integers or strings .sort or .sort.reverse would indeed suffice. Where the array contains someting more complex e.g. an employee record (where there are multiple pieces of data in each array element) then it’s not at all obviouse how the sort algorithm should work - in this case you would indeed need to specify this using block parameters. I think the use of the block paramaters in this exercise is there to build good habits going forwards.

1 Like

Hi, maybe that’s a bug but it works for me if I use

puts books.sort! { |first, second|
second <=> first }

but it doesn’t sort descendant with

puts books.sort! do |first, second|
second <=> first

1 Like

Hi, I was also confused on this tutorial and the explanations on the forum were not coherent with the mechanics of the output.

book_1 = “A Wrinkle In Time”
book_2 = “A Brief History Of Time”

book_1 <=> book_2

Initially, you think book_1 has a lesser value as the string has less elements than book_2?!
No, it returns 1, meaning it has a greater value?

Then I found out it will evaluate alphabetically and each letter has a value.
book_ 1 = A W
book_ 2 = A B

If each letter in alphabet is assigned a value than A == A but W > B
A=0, B=1, C=2, D=3, E=4, F=5, G=6 etc

So if this logic is true:
book_1 = “A long road”
book_2 = “Baffled by forums”
book_3 = “Choose to work it our yourself”

book_1 <=> book_3 # returns -1 (if A=0, C=3)
book_2 <=> book_3 # returns -1 (if B=2, C=3)
book_3 <=> book_1 # returns 1 (if C=3, A=0)

Try it out and I guarantee it will synch with this logic…consistently.


How come we only have firstBook and secondBook when there are more than 2 books in the array? It doesn’t make sense.

1 Like

In any sorting operation there are only ever two objects being compared. The names are only parameters, as in placeholders. The iteration process supplies the values from the iterable.

1 Like