Created
August 10, 2011 02:37
-
-
Save zbelzer/1135960 to your computer and use it in GitHub Desktop.
Ruby's Non-stable sorting
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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