Skip to content

Instantly share code, notes, and snippets.

@zbelzer
Created August 10, 2011 02:37
Show Gist options
  • Save zbelzer/1135960 to your computer and use it in GitHub Desktop.
Save zbelzer/1135960 to your computer and use it in GitHub Desktop.
Ruby's Non-stable sorting
Item = Struct.new(:name, :position)
main = [Item.new('a', 0), Item.new('b', 0), Item.new('c', 1)]
sorted = [Item.new('d', 1), Item.new('e', 1), Item.new('f', 2)]
(main + sorted).sort_by {|item| item.position}
# My console output. Expected a, b then d, e and f with c anywhere in between them. YMMV
# [#<struct Item name="a", position=0>, #<struct Item name="b", position=0>, #<struct Item name="c", position=1>, #<struct Item name="e", position=1>, #<struct Item name="d", position=1>, #<struct Item name="f", position=2>]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment