Last active
December 10, 2015 12:08
-
-
Save akaptur/4431890 to your computer and use it in GitHub Desktop.
Surprised by stable sort
This file contains hidden or 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
def two_sort(ex): | |
ex_copy = ex[:] | |
ex.sort(key=lambda tup: tup[1]) | |
ex.reverse() | |
print "Ex: ", ex | |
ex_copy.sort(key=lambda tup: tup[1], reverse=True) | |
print "Copy:", ex_copy | |
assert ex == ex_copy # fails | |
if __name__ == '__main__': | |
ex = [('a', 0), ('b', 0), ('c', 2), ('d', 3)] | |
two_sort(ex) | |
# Ex: [('d', 3), ('c', 2), ('b', 0), ('a', 0)] | |
# Copy: [('d', 3), ('c', 2), ('a', 0), ('b', 0)] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment