Getting Technical 14/19


#1
> 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

Can we user book.sort.reverse! here ?


#2

That will work - not sure if it will work for getting through the tutorial section.... I think the answer they were looking for is to see you can reverse it like:

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


#3

That will work

book_1 <=> book_2


#4

No. First, you misspelled "books". Second, reverse! reverses in place, but you're reversing the result of sort, not books -- your code leaves books unchanged. This works:

books.sort!.reverse!

#5

Hi, i'm doing it this way....

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

numbers = [4,7,1,3,4,9,6]
puts alphabetize(numbers)

It works, but still say...
"Oops, try again. It looks like your method doesn't default to alphabetizing an array when it doesn't receive a second parameter."

------------------------------ Edit
i finally passed through the tutorial with this code

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

numbers = [4,7,1,3,4,9,6]
puts alphabetize(numbers)

Frankly, I do not quite understand why.


#6

the sorting is already done. Therefore books.reverse works. It is, however, not accepted.

gives: ["War and Peace", "Utopia", "Charlie and the Chocolate Factory", "A Wrinkle in Time", "A Brief History of Time"]

books.sort! { |secondBook, firstBook| firstBook <=> secondBook } is accepted. (line 4 as a guide).

gives: ["War and Peace", "Utopia", "Charlie and the Chocolate Factory", "A Wrinkle in Time", "A Brief History of Time"]


#7

So... what is the final code? All of the answers posted above are not accepted to pass the lesson.

I think I totally became more confused just reading this thread. Ugh...


#8

the simple answer is this

book_1 <=> book_2


#9

Can anyone explain me how below code going to sort the array in normal and reverse order...what does it compare...?
books.sort! { |firstBook, secondBook| firstBook <=> secondBook } # normal order

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


#10

books.sort!.reverse! works for me, make sure you add "!" on reverse


#11

yes, you can.:grinning:


#12

Final code is this:
books = ["Charlie and the Chocolate Factory", "War and Peace", "Utopia", "A Brief History of Time", "A Wrinkle in Time"]
books.sort! { |firstBook, secondBook| firstBook <=> secondBook }
books.reverse! { |firstBook, secondBook| firstBook <=> secondBook }


#13

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

This is what i used after trail and error as well as reading here.

J


#14

Hi,

The exercise is asking to use the example, if you do not want to use it, you can pass it using books.sort!.reverse!
If you want to follow the exercise use:
books.sort! { |firstBook, secondBook| secondBook <=> firstBook }

You changed the order.


#15

Can we simply put this code :

books = [“Charlie and the Chocolate Factory”, “War and Peace”, “Utopia”, “A Brief History of Time”, “A Wrinkle in Time”]
books.sort!
books.reverse!
puts books

Why do we have to put the { |firstBook, secondBook| firstBook <=> secondBook } part ?