13/19 I seriously cannot figure this out


#1

I appear to be the only one that's had issues on this, and the program's hints are "use .sort!" which is extremely useless. So, the code is:

# library sorting code
books = ["Charlie and the Chocolate Factory", "War and Peace", "Utopia", "A Brief History of Time", "A Wrinkle in Time"]

# How might we sort! the books in alphabetical order? (Hint, hint)

and I don't know that it's asking me to do to put it in alphabetical order


#2

Hey @mikenike528

All you need to do is like in the following example, by executing the sort! function on the books array, similar to the previous example where there array was of integers.
While an array of integers will sort by value of number, low -> big by default, running the same function on an array of strings will sort by alphabetical value by default.


#3

Pssst. Maybe you should try the "extremely useless" hint.


#4

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 book after second book
-1

else #first and second are equal
0
end
end


#5

Try, this:

books.sort!


#6

Hey guys, i just want to know and have a little bit of an understanding on why this code work for me?

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

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

i understand in the general sense that " fruits.sort! { |fruit1, fruit2| fruit1 <=> fruit2} " sorts in alphabetically, its this specific line that i don't 100% understand " |fruit1, fruit2| fruit1 <=> fruit2 "......