FAQ: Ordering Your Library - What You'll Be Building

This community-built FAQ covers the “What You’ll Be Building” exercise from the lesson “Ordering Your Library”.

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

Learn Ruby

FAQs on the exercise What You’ll Be Building

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, I’m just finishing up the “Ordering Your Library” lesson under Blocks and Sorting (section 5 in Ruby). It seems like this activity was to build a method to alphabetize, but the introduction paragraph on the first page (1 of 6) says that we’d be building a reverse alphabetize method!

Anyone else messed up by that introductory page?
Thanks for any insights.

1 Like

I have some problem understanding the code in the control flow:

if rev
     arr.sort { |item1, item2| item2 <=> item1 }
     arr.sort { |item1, item2| item1 <=> item2 }

Why do we get a reversed array from the else clause when item1 and item2 switch positions around the spaceship operator?

According to the instructions, the spaceship operator <=> returns 1, 0, or -1. Does the operator do anything else more than return a numeric value?

1 Like

Can I get some help understanding the extra steps this and the last chapter have us doing to sort and then reverse sort? I am brand new to Ruby, but I quickly realized that “array.sort!” and then “array.reverse!” sorts everything in ascending order and then in descending order. Why do these chapters have us doing the block code and all the extra steps? What am I missing?

1 Like

You are correct in that if our intent is simply to sort integers or strings in ascending/descending order, then the built-in available sort and reverse methods are perfectly suitable for the task and we don’t need these blocks of code.

However, the intent behind the exercises seems to be to equip us to handle more complex situations. If the sorting criteria is more convoluted, then we will need to write our own custom blocks of code. By understanding the simple and straightforward ascending/descending order blocks, we can familiarize ourselves us with the basic structure of the blocks and then we can implement our own logic/conditions for more complex sorting.

The basic structure of the blocks is that the sort method is going to go through an array. It will compare two elements at a time. We can either use the <=> operator or if-else conditions to handle different situations. Either of -1, 0 or 1 will be returned. If 1 is returned, then we will swap the two elements. If 0 or -1 is the returned, then we leave the elements in their current positions. The blocks in the exercise for ascending/descending sort familiarize us with this structure for the simple and straightforward situation. Once we understand the basic structure, we can write our own blocks with more complex conditions and rules.

For the simple sorting of strings, you are correct. array.sort! and array.reverse! without any blocks of code are suitable. For more about the sort method, here is a useful and detailed explanation by Tony DiNitto

1 Like