Bird
Raised Fist0
Rubyprogramming~10 mins

Array sorting and reversing in Ruby - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Array sorting and reversing
Start with array
↓
Call sort method
↓
Compare elements
↓
Rearrange elements in ascending order
↓
Sorted array ready
↓
Call reverse method
↓
Swap elements from ends towards center
↓
Reversed array ready
↓
End
The array is first sorted by comparing and rearranging elements in ascending order, then reversed by swapping elements from the ends towards the center.
Execution Sample
Ruby
arr = [3, 1, 4, 2]
sorted_arr = arr.sort
reversed_arr = sorted_arr.reverse
puts reversed_arr
This code sorts the array in ascending order, then reverses it, and prints the reversed array.
Execution Table
StepActionArray StateOutput/Result
1Initialize arr[3, 1, 4, 2]None
2Call arr.sort[3, 1, 4, 2]Returns [1, 2, 3, 4]
3Assign sorted_arr[1, 2, 3, 4]None
4Call sorted_arr.reverse[1, 2, 3, 4]Returns [4, 3, 2, 1]
5Assign reversed_arr[4, 3, 2, 1]None
6Print reversed_arr[4, 3, 2, 1]4 3 2 1
7End[4, 3, 2, 1]Program ends
💡 All steps completed, program ends after printing reversed array.
Variable Tracker
VariableStartAfter Step 2After Step 4Final
arr[3, 1, 4, 2][3, 1, 4, 2][3, 1, 4, 2][3, 1, 4, 2]
sorted_arrnilnil[1, 2, 3, 4][1, 2, 3, 4]
reversed_arrnilnilnil[4, 3, 2, 1]
Key Moments - 3 Insights
Why does arr.sort not change the original array 'arr'?
Because in Ruby, 'sort' returns a new sorted array and does not modify the original array. This is shown in execution_table step 2 where 'arr' stays the same but 'sorted_arr' gets the sorted values.
What does reverse do to the array?
The 'reverse' method returns a new array with elements in opposite order. In step 4, 'sorted_arr' remains ascending, but 'reversed_arr' gets the reversed order.
Why do we assign the result of sort and reverse to new variables?
Because both 'sort' and 'reverse' return new arrays without changing the original. Assigning to new variables keeps the results accessible, as seen in steps 3 and 5.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of 'sorted_arr' after step 2?
Anil
B[3, 1, 4, 2]
C[1, 2, 3, 4]
D[4, 3, 2, 1]
💡 Hint
Check variable_tracker for 'sorted_arr' after step 2; it is assigned after step 3.
At which step does the array become reversed?
AStep 2
BStep 4
CStep 3
DStep 6
💡 Hint
Look at execution_table step 4 where reverse is called and reversed_arr is assigned.
If we changed 'arr.sort' to 'arr.sort!' (with exclamation), how would 'arr' change after step 2?
AIt would remain [3, 1, 4, 2]
BIt would become [4, 3, 2, 1]
CIt would become [1, 2, 3, 4]
DIt would become nil
💡 Hint
In Ruby, methods ending with '!' modify the original object, so 'arr' would be sorted in place.
Concept Snapshot
Array sorting and reversing in Ruby:
- Use arr.sort to get a new sorted array (original unchanged).
- Use arr.sort! to sort in place (modifies original).
- Use arr.reverse to get a new reversed array.
- Use arr.reverse! to reverse in place.
- Assign results to variables to keep changes.
Full Transcript
This visual trace shows how Ruby arrays can be sorted and reversed. Starting with an array [3, 1, 4, 2], calling 'sort' returns a new sorted array [1, 2, 3, 4] without changing the original. Then calling 'reverse' on the sorted array returns a new reversed array [4, 3, 2, 1]. Variables track these changes step by step. Key points include understanding that 'sort' and 'reverse' return new arrays, so assigning their results is necessary to keep changes. The quiz tests understanding of when variables change and the difference between methods that modify in place and those that don't.

Practice

(1/5)
1. What does the Ruby method sort do when called on an array?
easy
A. It arranges the elements in ascending order.
B. It reverses the order of elements.
C. It removes duplicate elements.
D. It shuffles the elements randomly.

Solution

  1. Step 1: Understand the purpose of sort

    The sort method arranges elements in ascending order by default.
  2. Step 2: Compare with other methods

    Unlike reverse which flips order, sort orders elements from smallest to largest.
  3. Final Answer:

    It arranges the elements in ascending order. -> Option A
  4. Quick Check:

    sort = ascending order [OK]
Hint: Remember: sort arranges ascending, reverse flips order [OK]
Common Mistakes:
  • Confusing sort with reverse
  • Thinking sort removes duplicates
  • Assuming sort shuffles elements
2. Which of the following is the correct syntax to reverse an array arr in Ruby?
easy
A. arr.reverse!
B. arr.reverse[]
C. reverse.arr()
D. arr.reverse()!

Solution

  1. Step 1: Identify the correct method call

    The method to reverse an array in place is reverse! called on the array object.
  2. Step 2: Check syntax correctness

    Only arr.reverse! is valid Ruby syntax; others are incorrect method calls or misplaced symbols.
  3. Final Answer:

    arr.reverse! -> Option A
  4. Quick Check:

    reverse! modifies array in place [OK]
Hint: Use reverse! to reverse array in place [OK]
Common Mistakes:
  • Using parentheses incorrectly
  • Placing exclamation mark after parentheses
  • Calling reverse as a standalone function
3. What is the output of the following Ruby code?
arr = [3, 1, 4, 2]
sorted_arr = arr.sort
reversed_arr = sorted_arr.reverse
puts reversed_arr.inspect
medium
A. [1, 2, 3, 4]
B. [3, 1, 4, 2]
C. [4, 3, 2, 1]
D. [2, 4, 1, 3]

Solution

  1. Step 1: Sort the original array

    Calling arr.sort returns a new array sorted ascending: [1, 2, 3, 4].
  2. Step 2: Reverse the sorted array

    Calling sorted_arr.reverse flips the order to [4, 3, 2, 1].
  3. Final Answer:

    [4, 3, 2, 1] -> Option C
  4. Quick Check:

    sort then reverse = descending order [OK]
Hint: Sort then reverse = descending order [OK]
Common Mistakes:
  • Assuming reverse modifies original array
  • Confusing sorted array with original
  • Expecting reverse to sort ascending
4. Identify the error in this Ruby code snippet:
arr = [5, 2, 9]
arr.sort!
arr.reverse
puts arr.inspect
medium
A. Missing parentheses in method calls cause errors.
B. The sort! method is invalid syntax.
C. The array arr cannot be reversed after sorting.
D. Using reverse does not change arr in place.

Solution

  1. Step 1: Understand sort! effect

    sort! sorts the array arr in place, so arr becomes [2, 5, 9].
  2. Step 2: Check reverse method usage

    reverse returns a new reversed array but does not modify arr itself.
  3. Final Answer:

    Using reverse does not change arr in place. -> Option D
  4. Quick Check:

    reverse without ! returns new array [OK]
Hint: Use reverse! to modify array in place [OK]
Common Mistakes:
  • Assuming reverse changes original array
  • Thinking sort! is invalid
  • Believing method calls need parentheses always
5. Given an array arr = [7, 3, 5, 3, 9, 1], how can you create a new array that is sorted in descending order without changing the original array?
hard
A. Use arr.sort! then arr.reverse!
B. Use arr.sort.reverse
C. Use arr.reverse.sort
D. Use arr.reverse! then arr.sort!

Solution

  1. Step 1: Sort the array without modifying original

    arr.sort returns a new sorted array ascending, original arr stays unchanged.
  2. Step 2: Reverse the sorted array to get descending order

    Calling reverse on the sorted array returns a new array reversed, still not changing original.
  3. Final Answer:

    Use arr.sort.reverse -> Option B
  4. Quick Check:

    sort then reverse = descending new array [OK]
Hint: Chain sort then reverse to get descending without changing original [OK]
Common Mistakes:
  • Using sort! or reverse! which modify original array
  • Reversing before sorting
  • Expecting reverse.sort to work the same
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard