# FAQ: Ordering Your Library - Sorting With Control Flow

This community-built FAQ covers the “Sorting With Control Flow” exercise from the lesson “Ordering Your Library”.

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

## FAQs on the exercise Sorting With Control Flow

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 () 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.

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

Need broader help or resources? Head here.

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

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!

rev=false is a parameter and it’s default when no input is given
so when we say if rev=true I am confused - is rev=false true when there is no input? and rev=false is false when we give that parameter a value?

1 Like
``````    rev=false

``````

is the case when there is no second positional argument. It would make little sense to call the method, `method(something, false)` since that is already the default.

However, if we include `true` as a second argument, the return will be reversed.

2 Likes

I’m having trouble reversing the order of my array, below is the only way I was able to do it:

def alphabetize(arr, rev=false)
arr.sort!
if rev = true
arr.reverse!
else
return arr
end
end

numbers = [7, 6, 2, 4, 8]
numbers.reverse!
puts alphabetize(numbers)

However an error comes up on the console saying I should use == instead of = for rev = true. If I change it to = then it no longer reverses.

``````def alphabetize(arr, rev=false)
arr.sort!
if rev = true
arr.reverse!
else
return arr
end
end
``````

The return should not be in an else clause, else the reversed array does not get returned. What if we write it like so…

``````def alphabetize(arr, rev=false)
arr.sort!
if rev == true    # note relational operator
arr.reverse!
end
arr               # implicit return of last line in block
end
``````

Now both forms of the array are returned.

4 Likes

hi mtf why are there 2 end in this code?

The inner `end` is the closing of the `if` block, the outer `end` is the closing of the method block.

1 Like

thnx mtf your the best

‫בתאריך שבת, 7 בספט׳ 2019 ב-19:35 מאת ‪Roy via Codecademy Forums‬‏ <‪[email protected]‬‏>:‬

2 Likes

def alphabetize (arr, rev=false)
arr.sort!
if rev
arr.reverse!
end
end

numbers = [6,2, 7]
puts alphabetize(numbers)

This snippet does not print the asc array. I noticed it needs an explicit else block to produce an output. Why is that ?

If you look at the example a couple of posts up in the thread you will see there is no else.

``````def alphabetize(arr, rev=false)
arr.sort!
if rev == true
arr.reverse!
end
arr
end
``````

We only need to add `arr` before the final `end` so that object is returned to the caller.

mtf, thanks again!
What confused me is that this returns the arr
def alphabetize(arr, rev=false)
arr.sort!
end

but this needs an explicit arr in the last line to return the array.

``````def alphabetize(arr, rev=false)
arr.sort!
if rev == true
arr.reverse!
end
arr
end``````

That’s so the arr is returned in any case, whether reversed or not.

1 Like

I am stuck with this exercise as well. Not sure I understood the instructions correctly. What do they mean by after your sort! call…which one are they referring too? what part of the code? the top part or the one under my arrays numbers? I am confused.

Thank you in advance for any help!

1.
Instructions:

1/After your `.sort!` call, add an `if-else` statement. If `rev` is true, call `reverse!` on `arr` , `else` return `arr` .
Keep your `numbers` array and the `puts` statement so that you can see your work in action!
Code:

def alphabetize(arr, rev=false)
if rev
arr.sort!{|item1, item2| item2<=>item1}
else
arr.sort!{|item1, item2| item1<=>item2}
end
end

numbers=[10, 12, 35, 17]
numbers.sort!
if rev==true
numbers.reverse!
else
return arr
end
puts numbers

Thanks for this explanation, mtf!

I think that the instructions are a little bit confusing, because it says:

After your .sort! call, add an if-else statement. If rev is true, call reverse! on arr, else return arr.

The else statement is the most confusing part.

So:

This means exactly the same as " else return arr. ", but if you actually use return here, you get no results.

I am having difficulties to understand `return`, but your explanation helped me to understand it better now.

Still,

I’m still stuck on this (possibly bc English is not my first language).
When adding `arr` after closing your if block, shouldn’t the def method get only this last information?

When we look at the first example, there is a return in the else clause but none in the if clause. We account for that by not having an else clause, only an if, with its reverse action taking place immediately on the array. Once that action is complete we arrive at the one return that hands back either result. Note that since it is the last line in the method block, `return` is implicit so we don’t need to write it.

``````    arr
end
``````

and

``````    return arr
end
``````

are the same thing.

1 Like

thank you very much! now I get it ^^

Edit: just got to the implicit return exercise yay

1 Like

So Ruby implicitely return the last line?

of the method, just as a block also has an implicit return. The last line is an expression (a value).

``````def pi
Math::PI.round(5)
end
puts pi
# 3.14159
``````

We learn at some point that this is the way Ruby prefers variables to be defined.

Let’s consider an example where implicit return is used extensively…

``````def pi
Math::PI.round(5)                 # here
end

class Circle
end
end
def circumference
2 * pi * radius                 # here
end
def area
pi * radius ** 2                # here
end
end
``````
``````def unit_circle
unit_circle = Circle.new(1)
#                                 and here
<<-OUTPUT
Unit Circle
#{'=' * 30}
Circumference: #{unit_circle.circumference} units
Area: #{unit_circle.area} square units
#{'=' * 30}
OUTPUT
end

puts unit_circle
``````

implicit return - Replit

Hey,

Why does this say ants bees cockroaches? I’m confused!

That is the output of the lesson checker. No cause for confusion. A lot of times the SCT (Submission correctness Test) built into the lesson will run our code with preset inputs.