Great! Now let’s add a little logic to our method.
numbers = [5, 1, 3, 8] numbers.sort! puts numbers
- In the above example, we create a new array called
- Then, we sort the array.
- Finally, we print out
8, the sorted array.
In Ruby, there are two sorting methods,
sort!. The first method,
.sort, simply returns a sorted array while leaving the original array alone. The second method,
.sort!, modifies the actual array.
Inside your method, add a line that calls
.sort! on the
arr array. Since it is the last line of the method, the sorted array will be returned.
Now test your work! After your method, make a new array called
numbers that contains a few numbers like the example above.
alphabetize method with your
numbers array as the parameter and
puts the result to the console.