Sorting With Control Flow - help Oops, try again. It looks like your method doesn't default to alphabetizing an array when it doesn't receive a second parameter


#1

def alphabetize(arr, rev=false)
if rev
arr.sort { |item1, item2| item2 <=> item1 }
else
arr.reverse! { |item1, item2| item1 <=> item2 }
end
end
numbers = [3, 9, 7, 4, 1]
puts "1-10: #{alphabetize(numbers)}"
puts "10-1: #{alphabetize(numbers, true)}"


#2

Found the answer here http://discuss.codecademy.com/t/part-5-small-bug-problem-with-arr-sort/1139

Just return arr after the if block.


#3

I have that person's problem. this is the code which I USED your solution on:

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

numbers = [42, 1816, 43, 141, 1680, 678, 244]
alphabetize(numbers, true)

Is there anything wrong with this?
:rage:


#4

I guess your "puts arr" will never be executed, as the alphabetize method will return in any of your if/else before that.
You should try removing the returns in the if/else, put it in place of your "puts arr", and move your "puts arr" outside of the method, at the end of the program.


#6

What should the code look like after that?
because my code looks like this:

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

numbers = [42, 1816, 43, 141, 1680, 678, 244]
alphabetize(numbers)

It returns this on the console:
42
43
141
244
678
1680
1816
nil

and this is the error message:
Oops, try again. It looks like your method doesn't default to alphabetizing an array when it doesn't receive a second parameter.

:rage:


#7

First I tried to just return arr, then (outside of alphabetize) puts numbers:

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

numbers = [42, 1816, 43, 141, 1680, 678, 244]
alphabetize(numbers)
puts(numbers)

This will give another error:

It looks like you didn't use the .reverse! method.

So, with using reverse! this is:

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

numbers = [42, 1816, 43, 141, 1680, 678, 244]
alphabetize(numbers)
puts(numbers)

And this is working.
On a side note, this could be shorten like this:

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

There is actually no need for the <=> thing


#8

This is more related to the section before it but it may also help here:
I told the method to return arr and then puts numbers at the end after calling the method. I assume this passes the modified arr (since it's using .sort!) to the local variable numbers and then puts the modified numbers array.


#9

is there a reason why

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

wouldn't work? Seems a lot more elegant than the accepted solution.


#10

I'm not sure if below is right code, but I got passed the lesson.

def alphabetize(arr, rev=false)
arr.sort!
if rev==true
arr.reverse!
end
numbers = [2, 4, 6, 7]
alphabetize(numbers)
puts numbers
end


#11

in the first example they had the 2nd "end" before the "def" line.
when formatting for this assignment, it would not succeed until after I moved "end" to the very end.

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

original location for end

numbers=[1, 9, 3, 7, 5]

alphabetize(numbers)
puts numbers
end

^-- where the assignment accepted the "end" statement

anyone have an idea as to why?


#12

I was able to get it working with return as well, but I find that a little odd in this case since in part 5 you are instructed to sort and reverse with !, which modifies the original array instead of creating a new one. so if your puts statement is outside the method, it works as designed without a return statement.


#13

I see a lot of folks maintaining arr.sort! in front of their if statements. That means it will execute that first which is what is causing the problem. If you move that to your else statement that will fix it.

def alphabetize(arr, rev=false)
    if rev == true
        arr.reverse!
    else
        arr.sort!
    end
end
arr = [2, 3, 1]
puts alphabetize(arr)

#14

The instructions says

After your .sort! call, add an if statement

So .sort! should definitely be outside an if statement.

My code didn't work though until I added a return statement.

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

There's definitely something wrong with the instructions or how the code is being tested for this part.


#15

YES! I read every post here and i don;t see a convincing explanation of why this doesn't work. The original example doesn;t return arr, so i don;t know why it is needed. Problem with CC parser? I think this (dmc above) should work:
def alphabetize(arr, rev=false)
arr.sort!
if rev
arr.reverse!
end
end


#16

I was following the instruction as stated too and placing the IF statement after the arr.sort!. That was not working for me, so I moved my arr.sort! into my IF statement, as @mark.hollingsworth suggested, and it passed. Thanks!

My Solution:

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

numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9]

puts "A-z: #{alphabetize(numbers)}"
puts "Z-a: #{alphabetize(numbers, true)}

#17

This is what ended up working for me. I was also having the problem of it saying that my second parameter didn't have a default variable.


#18

While my solution actually compiles in such a way the exercise completes...it turns out that's not the correct way to write it....this is:

def alphabetize(arr, rev=false)
    arr.sort!
    if rev == true
        arr.reverse!
    else
        arr.sort!
    end
end
arr = [2, 3, 1]
puts alphabetize(arr)

#19

Alot of answers here but you just need to follow the instructions, and after that(at the end of the method) add a 'return arr'.
Remember the lessons before, it states that the method needs to return a value, initially we didn't add 'return' cause we only have one statement (arr.sort!). If Ruby sees one statement he will return it, but if he sees more then one he needs your help.

This was my code:

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

numbers =[1,3,6,2,9,0]

puts alphabetize(numbers, true)

Hope it helped!


#20

I agree with @socarlosb.

arr.sort! doesn't go in an else branch - instructions aside, it's logical that the array should still be sorted even if we don't want it in reverse order.

The main issue is implicit vs. explicit return. It seems Ruby methods implicity return - meaning they return the evaluated value of the last line in the method (unless you explicitly type return #something). In the preceding exercise, the last line was arr.sort! which returned the sorted array, which can be put to console just fine.

However, by adding the if rev logic in this step, the last line is changed and so the implicit return gets changed. I don't know exactly what value is implicitly returned by the if statement when rev = false, but whatever it is doesn't seem to work. So, we need to explicitly return at the end with return arr, or just leave arr as the last line for implicit return to return the right thing again.

I think that should clarify...however if anyone does know what gets returned by if rev when rev = false, please let me know!


#21

Simple code that worked for me:

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

numbers=[5, 1, 3, 8, 22, 15]

puts alphabetize(numbers)

Shout out to @boardplayer44375 for recommending the var.