Skip to content

Instantly share code, notes, and snippets.

@tkfm-yamaguchi
Last active August 29, 2015 14:04
Show Gist options
  • Save tkfm-yamaguchi/cf8bab2909a3c75b8673 to your computer and use it in GitHub Desktop.
Save tkfm-yamaguchi/cf8bab2909a3c75b8673 to your computer and use it in GitHub Desktop.
easy sorting on ruby
class Array
def my_sort!
1.upto(size-1) do |i|
i.downto(1) do |j|
swap!(j, j-1) if less?(j, j-1)
end
end
end
def swap!(ind1, ind2)
self[ind1], self[ind2] = self[ind2], self[ind1]
end
def less?(ind1, ind2)
self[ind1] < self[ind2]
end
end
a = [10, 2030, 300, 9]
a.my_sort!
p a
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment