Sorting and reversing arrays help organize data so it's easier to find or use. Sorting arranges items in order, and reversing flips their order.
Array sorting and reversing in Ruby
class Array # Sorts the array in ascending order and returns a new array def sorted_array self.sort end # Reverses the array and returns a new array def reversed_array self.reverse end end
The sort method returns a new array sorted in ascending order.
The reverse method returns a new array with elements in reverse order.
numbers = [3, 1, 4, 2] sorted_numbers = numbers.sort puts sorted_numbers.inspect
words = ["banana", "apple", "cherry"] sorted_words = words.sort puts sorted_words.inspect
empty_array = []
reversed_empty = empty_array.reverse
puts reversed_empty.inspectsingle_element = [42] reversed_single = single_element.reverse puts reversed_single.inspect
This program shows the original array, then the sorted version, and finally the reversed version.
numbers = [5, 2, 9, 1, 7] puts "Original array: #{numbers.inspect}" sorted_numbers = numbers.sort puts "Sorted array: #{sorted_numbers.inspect}" reversed_numbers = numbers.reverse puts "Reversed array: #{reversed_numbers.inspect}"
Sorting an array usually takes time proportional to the number of elements times the logarithm of that number (O(n log n)).
Reversing an array takes time proportional to the number of elements (O(n)).
A common mistake is to expect sort or reverse to change the original array. They return new arrays instead.
Use sort! or reverse! if you want to change the original array.
Sorting arranges array elements in order, usually ascending.
Reversing flips the order of elements in the array.
Both methods return new arrays unless you use the versions with ! to modify in place.
Practice
sort do when called on an array?Solution
Step 1: Understand the purpose of
Thesortsortmethod arranges elements in ascending order by default.Step 2: Compare with other methods
Unlikereversewhich flips order,sortorders elements from smallest to largest.Final Answer:
It arranges the elements in ascending order. -> Option AQuick Check:
sort = ascending order [OK]
- Confusing sort with reverse
- Thinking sort removes duplicates
- Assuming sort shuffles elements
arr in Ruby?Solution
Step 1: Identify the correct method call
The method to reverse an array in place isreverse!called on the array object.Step 2: Check syntax correctness
Onlyarr.reverse!is valid Ruby syntax; others are incorrect method calls or misplaced symbols.Final Answer:
arr.reverse! -> Option AQuick Check:
reverse! modifies array in place [OK]
reverse! to reverse array in place [OK]- Using parentheses incorrectly
- Placing exclamation mark after parentheses
- Calling reverse as a standalone function
arr = [3, 1, 4, 2] sorted_arr = arr.sort reversed_arr = sorted_arr.reverse puts reversed_arr.inspect
Solution
Step 1: Sort the original array
Callingarr.sortreturns a new array sorted ascending: [1, 2, 3, 4].Step 2: Reverse the sorted array
Callingsorted_arr.reverseflips the order to [4, 3, 2, 1].Final Answer:
[4, 3, 2, 1] -> Option CQuick Check:
sort then reverse = descending order [OK]
- Assuming reverse modifies original array
- Confusing sorted array with original
- Expecting reverse to sort ascending
arr = [5, 2, 9] arr.sort! arr.reverse puts arr.inspect
Solution
Step 1: Understand
sort!effectsort!sorts the arrayarrin place, soarrbecomes [2, 5, 9].Step 2: Check
reversemethod usagereversereturns a new reversed array but does not modifyarritself.Final Answer:
Usingreversedoes not changearrin place. -> Option DQuick Check:
reverse without ! returns new array [OK]
reverse! to modify array in place [OK]- Assuming reverse changes original array
- Thinking sort! is invalid
- Believing method calls need parentheses always
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?Solution
Step 1: Sort the array without modifying original
arr.sortreturns a new sorted array ascending, originalarrstays unchanged.Step 2: Reverse the sorted array to get descending order
Callingreverseon the sorted array returns a new array reversed, still not changing original.Final Answer:
Usearr.sort.reverse-> Option BQuick Check:
sort then reverse = descending new array [OK]
- Using sort! or reverse! which modify original array
- Reversing before sorting
- Expecting reverse.sort to work the same
Ready to prove it?
Pass the test and this pattern moves unknown
