Sort_by for custom sorting
📖 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 pagesCreate a variable called
max_pages with the value 300Use
sort_by on books to sort by the number of pagesPrint 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