Skip to content

Instantly share code, notes, and snippets.

@akaptur
Last active December 10, 2015 12:08
Show Gist options
  • Save akaptur/4431890 to your computer and use it in GitHub Desktop.
Save akaptur/4431890 to your computer and use it in GitHub Desktop.
Surprised by stable sort
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