0
0
Rubyprogramming~10 mins

Sort_by for custom sorting in Ruby - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Sort_by for custom sorting
Start with array
Apply sort_by block
Transform each element to sort key
Sort elements by keys
Return sorted array
The array is transformed by a block to keys used for sorting, then sorted by those keys, returning a new sorted array.
Execution Sample
Ruby
words = ["apple", "pear", "banana"]
sorted = words.sort_by { |word| word.length }
puts sorted
Sorts the array of words by their length in ascending order.
Execution Table
StepElementSort Key (word.length)ActionResulting Array State
1"apple"5Calculate key["apple", "pear", "banana"]
2"pear"4Calculate key["apple", "pear", "banana"]
3"banana"6Calculate key["apple", "pear", "banana"]
4All keys calculated-Sort by keys["pear", "apple", "banana"]
5Return sorted array-Output sorted array["pear", "apple", "banana"]
💡 All elements transformed to keys and sorted by those keys, sorting ends.
Variable Tracker
VariableStartAfter 1After 2After 3Final
words["apple", "pear", "banana"]["apple", "pear", "banana"]["apple", "pear", "banana"]["apple", "pear", "banana"]["apple", "pear", "banana"]
sortednilnilnilnil["pear", "apple", "banana"]
Key Moments - 2 Insights
Why does sort_by use the block's return value instead of the element itself to sort?
sort_by transforms each element into a key using the block (see steps 1-3 in execution_table). It sorts by these keys, not the original elements, allowing custom sorting criteria.
Does sort_by change the original array?
No, as shown in variable_tracker, the original array 'words' stays the same. sort_by returns a new sorted array stored in 'sorted' (step 5).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 2, what is the sort key for "pear"?
A5
B4
C6
D3
💡 Hint
Check the 'Sort Key (word.length)' column at step 2 in execution_table.
At which step does the array get sorted by the keys?
AStep 4
BStep 2
CStep 3
DStep 5
💡 Hint
Look for the 'Action' column mentioning sorting in execution_table.
If we changed the block to sort by the last letter of each word, how would the sort keys change?
AKeys would be word lengths
BKeys would be the first character of each word
CKeys would be the last character of each word
DKeys would be the whole word
💡 Hint
sort_by uses the block return value as key; changing block changes keys (see concept_flow).
Concept Snapshot
sort_by { |element| key } transforms each element to a key
Sorts elements by these keys ascending
Returns new sorted array, original unchanged
Useful for custom sorting criteria
Example: array.sort_by { |x| x.length }
Full Transcript
This example shows how Ruby's sort_by method works. We start with an array of words. Each word is passed to the block, which returns the word's length as the sort key. The method collects these keys, then sorts the original elements by these keys. The original array remains unchanged. The result is a new array sorted by word length. This process is shown step-by-step in the execution table and variable tracker. Key moments clarify why sort_by uses keys and does not modify the original array. The quiz tests understanding of keys and sorting steps.