Using .reverse! doesn't always sorts properly, why?


#1

Here is the code:

def alphabetize(arr, rev=false)
if rev
arr.reverse!
else
arr.sort!
end
end
numbers = [2,1,5,3,4,7,8,6,8]

alphabetize(numbers, true)
puts numbers

The result will be:
8
6
8
7
4
3
5
1
2
nil

The code passes, but the numbers are obviously not ordered properly. Any ideas why?


#2

I'm no genius, but it could just be you are alphabetizing numbers..? Feel free to correct me because I'm curious as well.


#3

Hello Blindpupil,

you made an error in your code, and it does exactly what you asked of it.
It only sorts the code when rev=false, otherwise it only reverses the code and then exits the loop without going to the sorting part.


#6