Discover how a simple method can save you hours of sorting headaches!
Why Sort_by for custom sorting in Ruby? - Purpose & Use Cases
Imagine you have a list of people with their ages and you want to sort them by age, but the ages are mixed with other details. Doing this by hand means checking each person's age and moving them around manually.
Sorting manually is slow and easy to mess up. You might forget someone's age or move the wrong person. It's tiring and not practical when the list grows bigger.
Using sort_by lets you tell Ruby exactly how to sort your list by picking the age from each person. Ruby does the hard work quickly and correctly for you.
people.sort { |a, b| a[:age] <=> b[:age] }people.sort_by { |person| person[:age] }You can easily sort complex lists by any detail you want, making your code cleaner and faster.
Sorting a list of students by their test scores to find the top performers without mixing up their names or other info.
Manual sorting is slow and error-prone.
sort_by simplifies sorting by letting you pick the sorting key.
This makes your code easier to read and maintain.
Practice
sort_by do?Solution
Step 1: Understand the purpose of
sort_bysort_bysorts elements by a value you choose for each element, like length or age.Step 2: Compare with other array methods
Unlikesort!,sort_byreturns a new sorted array without changing the original.Final Answer:
Sorts elements based on a value you specify for each element. -> Option AQuick Check:
sort_by sorts by chosen value [OK]
- Thinking sort_by changes the original array
- Confusing sort_by with reverse or uniq
- Assuming sort_by removes duplicates
words by their length using sort_by?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 useswords.sort_by { |word| word.length }. Others have syntax errors or wrong usage.Final Answer:
words.sort_by { |word| word.length } -> Option CQuick Check:
sort_by needs a block with |variable| [OK]
- Passing arguments directly instead of using a block
- Using => instead of block braces
- Misplacing parentheses around block
people = [{name: "Anna", age: 30}, {name: "Bob", age: 25}, {name: "Cara", age: 35}]
sorted = people.sort_by { |person| person[:age] }
puts sorted.map { |p| p[:name] }Solution
Step 1: Understand sorting by age
The array is sorted by the :age key in ascending order: 25, 30, 35.Step 2: Map sorted array to names
After sorting, the order is Bob (25), Anna (30), Cara (35). Mapping names gives ["Bob", "Anna", "Cara"].Final Answer:
["Bob", "Anna", "Cara"] -> Option BQuick Check:
sort_by age ascending = [Bob, Anna, Cara] [OK]
- Assuming sort_by sorts descending by default
- Confusing keys in hash access
- Not mapping to names after sorting
items = ["apple", "pear", "banana"] sorted = items.sort_by do |item| item.length end puts sorted
Solution
Step 1: Check block syntax
The block usesdo |item| ... end, which is valid for sort_by.Step 2: Verify sort_by usage
sort_by accepts a block and returns a new array sorted by item length. No syntax or logic errors.Final Answer:
No error; code works correctly -> Option AQuick Check:
sort_by with do-end block is valid [OK]
- Thinking parentheses are mandatory around block
- Confusing sort_by with sort!
- Assuming sort_by modifies original array
products = [
{name: "Pen", price: 1.5, stock: 10},
{name: "Notebook", price: 2.0, stock: 0},
{name: "Eraser", price: 0.5, stock: 5}
]
How would you use sort_by to get products sorted by price but only include those with stock greater than zero?Solution
Step 1: Filter products with stock > 0
Useselectto keep only products wherestockis greater than zero.Step 2: Sort filtered products by price
Applysort_byon the filtered array to sort byprice.Final Answer:
products.select { |p| p[:stock] > 0 }.sort_by { |p| p[:price] } -> Option DQuick Check:
Filter then sort_by for correct result [OK]
- Trying to filter inside sort_by block
- Sorting by stock instead of price
- Selecting after sorting, which keeps unwanted items
Ready to prove it?
Pass the test and this pattern moves unknown
