Learn

Great! Now let’s add a little logic to our method.

numbers = [5, 1, 3, 8] numbers.sort! puts numbers
  1. In the above example, we create a new array called numbers.
  2. Then, we sort the array.
  3. Finally, we print out 1, 3, 5, 8, the sorted array.

In Ruby, there are two sorting methods, .sort or sort!. The first method, .sort, simply returns a sorted array while leaving the original array alone. The second method, .sort!, modifies the actual array.

Instructions

1.

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.

Call your alphabetize method with your numbers array as the parameter and puts the result to the console.

Take this course for free

Mini Info Outline Icon
By signing up for Codecademy, you agree to Codecademy's Terms of Service & Privacy Policy.

Or sign up using:

Already have an account?