Skip to content

Instantly share code, notes, and snippets.

@shirts
Created October 14, 2016 02:25
Show Gist options
  • Save shirts/ebbc36de631f1005bec50c42c6c3c992 to your computer and use it in GitHub Desktop.
Save shirts/ebbc36de631f1005bec50c42c6c3c992 to your computer and use it in GitHub Desktop.
Custom Ruby sort implementation
def sort(original)
sorted = []
original.each do |original_num|
if sorted.empty? || original_num > sorted.last
sorted << original_num
next
else
sorted.each do |sorted_num|
if original_num <= sorted_num
sorted.insert(sorted.index(sorted_num), original_num)
break
end
end
end
end
sorted
end
sort(['abc', 'c', 'a', 'b'])
sort([5,1,2,7,3,0,1,2,5])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment