How to sort an array without using sort method in Ruby?

def sort_descending(array)
return [] if array.empty?
is_sorted = true

while is_sorted
is_sorted = false

(array.size - 1).times do |i|
if array[i] < array[i + 1]
array[i], array[i + 1] = array[i + 1], array[i]
is_sorted = true
end
end
end
array
end
number_array = [57,2,30, 4, 1, 45, 8, 11]
char_array = ['d', 'v', 'r', 'f', 'b', 'a', 'p', 'x', 'z']
2.5.3 :361 > sort_descending(number_array)
=> [57, 45, 30, 11, 8, 4, 2, 1]

2.5.3 :362 > sort_descending(char_array)
=> ["z", "x", "v", "r", "p", "f", "d", "b", "a"]
def sort_ascending(array)
return [] if array.empty?
is_sorted = true

while is_sorted
is_sorted = false

(array.size - 1).times do |i|
if array[i] > array[i + 1]
array[i], array[i + 1] = array[i + 1], array[i]
is_sorted = true
end
end
end
array
end
number_array = [57,2,30, 4, 1, 45, 8, 11]
char_array = ['d', 'v', 'r', 'f', 'b', 'a', 'p', 'x', 'z']
2.5.3 :361 > sort_ascending(number_array)
=> [1, 2, 4, 8, 11, 30, 45, 57]

2.5.3 :362 > sort_ascending(char_array)
=> ["a", "b", "d", "f", "p", "r", "v", "x", "z"]

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store