Skip to content

Instantly share code, notes, and snippets.

@giwa
Created May 15, 2016 07:27
Show Gist options
  • Save giwa/b1ef8d0a3d9b0353eed37ffcf4571e91 to your computer and use it in GitHub Desktop.
Save giwa/b1ef8d0a3d9b0353eed37ffcf4571e91 to your computer and use it in GitHub Desktop.
>>> def compare(a, b):
... """Comparison that ignores the first letter"""
... return cmp(a[1:], b[1:])
>>> names = ['Adam', 'Donald', 'John']
>>> names.sort(cmp=compare)
>>> names
['Adam', 'John', 'Donald']
>>> names = ['Adam', 'Donald', 'John']
>>> names.sort(key=lambda x: x[1:])
>>> names
['Adam', 'John', 'Donald']
>>> l = [('betty', 1), ('a', 1), ('butter', 2)]
>>> def custom_key(x):
... return -x[1], x[0]
...
>>> l.sort(key=custom_key)
>>> l
[('butter', 2), ('a', 1), ('betty', 1)]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment