Learn

Methods, Blocks, & Sorting

Foundations

If we were to hand you five books and ask you to arrange them, sorted by title, on a shelf, how would you do it?

Most sorting algorithms assume we are sorting an array of items, which involves comparing any two items in the array and deciding which should come first.

For our books example, if for any pair we always chose the book whose title came first in the alphabet, we could devise a strategy for sorting. These “strategies” are the sorting algorithms mentioned in the previous example. Our job is to decide how to compare two items in the list, and let Ruby decide which strategy to use.

What Ruby method could we call on `books`

in order to sort the list of books alphabetically? Try it out in the editor.