Skip to content

Instantly share code, notes, and snippets.

@ibeauregard
Last active June 12, 2021 15:40
Show Gist options
  • Save ibeauregard/d9638591a0c56e5436301efc4b3419c5 to your computer and use it in GitHub Desktop.
Save ibeauregard/d9638591a0c56e5436301efc4b3419c5 to your computer and use it in GitHub Desktop.
from collections import namedtuple
Key = namedtuple('Key', ['index', 'ascending'])
keys = [Key(index=2, ascending=False), Key(index=0, ascending=True)]
def sort_key(row):
values = []
for key in keys:
value = converted(row[key.index])
values.append(value if key.ascending else -value)
return tuple(values)
print(sort_key([1, 2, 3, 4]))
# Output: (-3, 1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment