The following code returns a sorted array in ascending order:
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 }
How does this work?
First it compares two objects, then it returns either 0, 1, or -1.
But how does sort
know how to sort?