0
0
Rubyprogramming~5 mins

Array sorting and reversing in Ruby - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
How do you sort an array in Ruby in ascending order?
Use the sort method. For example, [3, 1, 2].sort returns [1, 2, 3].
Click to reveal answer
beginner
How do you reverse the order of elements in an array in Ruby?
Use the reverse method. For example, [1, 2, 3].reverse returns [3, 2, 1].
Click to reveal answer
intermediate
What does the sort! method do in Ruby?
It sorts the array in place, changing the original array instead of creating a new one.
Click to reveal answer
intermediate
How can you sort an array in descending order in Ruby?
You can use sort.reverse or sort { |a, b| b <=> a } to sort in descending order.
Click to reveal answer
intermediate
What is the difference between reverse and reverse! methods?
reverse returns a new array with elements reversed, while reverse! reverses the elements in the original array itself.
Click to reveal answer
Which Ruby method sorts an array without changing the original array?
Areverse
Bsort
Csort!
Dreverse!
What does [5, 3, 8].reverse return?
A[8, 5, 3]
B[8, 3, 5]
C[3, 5, 8]
D[5, 3, 8]
How do you sort an array in descending order in Ruby?
Aarray.reverse!
Barray.reverse.sort
Carray.sort!
Darray.sort.reverse
Which method changes the original array when reversing it?
Areverse!
Breverse
Csort
Dsort!
What does sort! do?
ASorts and returns a new array
BDoes nothing
CSorts the original array in place
DReverses the array
Explain how to sort and reverse an array in Ruby, including how to do it without changing the original array.
Think about methods that return new arrays versus those that change the original.
You got /4 concepts.
    Describe the difference between sort and sort!, and when you might want to use each.
    Consider if you want to keep the original array unchanged or not.
    You got /4 concepts.