OneBite.Dev - Coding blog in a bite size

sort items in array by asc in Ruby

Code snippet on how to sort items in array by asc in Ruby

array = [5, 4, 6, 3, 2, 1]

array.sort!

This code will sort the elements in an array in ascending order. First, the array is declared with various elements inside it. Then the command “.sort!” sorts the elements in the array in ascending order, starting with the lowest number. After the command is executed, the array will look like this: [1, 2, 3, 4, 5, 6]. The ”!” behind “.sort” is used to modify the original array, instead of returning a new, sorted array.

ruby