# 19 Why does this work, but isn't accepted?

#1

Here's my code:

fruits = ["orange", "apple", "banana", "pear", "grapes"]
puts fruits.sort!.reverse!

and it works, but an error pops up, saying: Oops, try again. Make sure to use either <=> or else/elsif/else to compare and sort your fruits.

So I just threw a <=> in there

fruits = ["orange", "apple", "banana", "pear", "grapes"]
"orange" <=> "apple"
puts fruits.sort!.reverse!

And I get the same result, but it's accepted... but why won't it work without the "orange" <=> "apple" bit in there?

#2

Use `.sort!` to sort the fruits array in descending (that is, reverse) alphabetical order. You can use the combined comparison operator (like the example above) or an `if/elsif/else` statement.

This would imply that we should be practicing on the new material, rather than reach for a built-in.

Eg.

``fruits.sort! { |fruit1, fruit2| fruit2 <=> fruit1 }``

or

``````fruits.sort! do |fruit1, fruit2|
fruit2 <=> fruit1
end``````

#3

Curious what the if/elsif/else solution would look like for this level of ruby coding?

Would it just be mocking the functionality of the <=> operator like this?

`````` fruits = ["orange", "apple", "banana", "pear", "grapes"]

fruits.sort! do |fruit_1, fruit_2|
if( fruit_1 < fruit_2)
retval = 1
elsif( fruit_1 == fruit_2)
retval = 0
else( fruit_1 > fruit_2)
retval = -1
end
retval
end``````