15/19


#1

Hi Guys ,
can anyway tell me why my code is not accepted
i have sorted the books in a descending order :["War and Peace", "Utopia", "Charlie and the Chocolate Factory", "A Wrinkle in Time", "A Brief History of Time"] but it was not accepted this is my code
books = ["Charlie and the Chocolate Factory", "War and Peace", "Utopia", "A Brief History of Time", "A Wrinkle in Time"]

To sort our books in ascending order, in-place

books.sort! { |firstBook, secondBook| firstBook <=> secondBook }

Sort your books in descending order, in-place below

books.sort do |first, second|
if first > second # first book before second alphabetically
-1
elsif first < second # first after second
1
else # first and second are the same
0
end
end


#2

i've figured it out i simply forgot the ! after sort


#3

The code proposed above didn't work for me. This one works just fine.
books.sort! { |firstBook, secondBook| secondBook <=> firstBook}
Sheers


#4

First, your code is way more complicated than it needs to be ... you're supposed to use the <=> operator. Second, your comments are wrong: "if first > second # first book before second alphabetically" -- if first is > second, it comes after second alphabetically; you return -1 because you want first to be sorted before second when it comes after second alphabetically.


#5

Yeah, I have the same problem too. I try the code in the Hint, but it still does right. It says please use .sort! instead of .sort?


#6

use this code
books.sort! do |first, second|
if first < second
1
elsif first > second
-1
else
0
end
end


#7

The following worked for me, but wasn't accepted either

books.sort!.reverse

#8

Why can't we just use books.reverse!

It was accepted for me, but I suppose they want us to learn the other way for a reason?


#9

I like Bennanni and warbs answers. This works too.

books.sort! do |first, second|
    second <=> first
end