📖 Scenario: You work in a bookstore. You have a list of books with their titles and number of pages. You want to sort the books by their page count to help customers find shorter or longer books easily.
🎯 Goal: Build a Ruby program that sorts a list of books by their page count using sort_by.
📋 What You'll Learn
Create an array of hashes called books with exact entries for title and pages
Create a variable called max_pages with the value 300
Use sort_by on books to sort by the number of pages
Print the sorted list of book titles and their page counts
💡 Why This Matters
🌍 Real World
Sorting lists of items by a property is common in apps like bookstores, libraries, or online shops to help users find what they want quickly.
💼 Career
Knowing how to use sort_by for custom sorting is a basic but important skill for Ruby developers working with collections of data.
Progress0 / 4 steps
1
Create the books array
Create an array called books with these exact hashes: { title: 'Ruby Basics', pages: 150 }, { title: 'Advanced Ruby', pages: 320 }, { title: 'Rails Guide', pages: 280 }, { title: 'JavaScript Essentials', pages: 200 }
Ruby
Hint
Remember to create an array with hashes inside, each hash having keys :title and :pages.
2
Add max_pages variable
Create a variable called max_pages and set it to 300
Ruby
Hint
Just create a variable named max_pages and assign 300 to it.
3
Sort books by pages using sort_by
Use sort_by on books to create a new array called sorted_books sorted by the :pages key
Ruby
Hint
Use sort_by with a block that returns book[:pages].
4
Print the sorted books
Use a for loop with variable book to iterate over sorted_books and print each book's title and pages in the format: "Title: Ruby Basics, Pages: 150"
Ruby
Hint
Use a for loop and puts with string interpolation to print each book's details.
Practice
(1/5)
1. What does the Ruby method sort_by do?
easy
A. Sorts elements based on a value you specify for each element.
B. Changes the original array to a sorted one.
C. Removes duplicate elements from an array.
D. Reverses the order of elements in an array.
Solution
Step 1: Understand the purpose of sort_by
sort_by sorts elements by a value you choose for each element, like length or age.
Step 2: Compare with other array methods
Unlike sort!, sort_by returns a new sorted array without changing the original.
Final Answer:
Sorts elements based on a value you specify for each element. -> Option A
Quick Check:
sort_by sorts by chosen value [OK]
Hint: Remember: sort_by sorts by a custom value, not in-place [OK]
Common Mistakes:
Thinking sort_by changes the original array
Confusing sort_by with reverse or uniq
Assuming sort_by removes duplicates
2. Which of the following is the correct syntax to sort an array words by their length using sort_by?
easy
A. words.sort_by(word.length)
B. words.sort_by => word.length
C. words.sort_by { |word| word.length }
D. words.sort_by(word) { length }
Solution
Step 1: Recall Ruby block syntax for sort_by
The correct syntax uses a block with curly braces and a block variable: array.sort_by { |item| expression }.
Step 2: Match syntax with the options
words.sort_by { |word| word.length } correctly uses words.sort_by { |word| word.length }. Others have syntax errors or wrong usage.
Final Answer:
words.sort_by { |word| word.length } -> Option C
Quick Check:
sort_by needs a block with |variable| [OK]
Hint: Use curly braces and |var| for sort_by blocks [OK]
Common Mistakes:
Passing arguments directly instead of using a block