Skip to content

Instantly share code, notes, and snippets.

@huklee
Created May 30, 2018 00:49
Show Gist options
  • Save huklee/6d97f1df1cc8d002f1f39ff284c63c95 to your computer and use it in GitHub Desktop.
Save huklee/6d97f1df1cc8d002f1f39ff284c63c95 to your computer and use it in GitHub Desktop.
how to sort by 2 keys for list in python3
import functools
def sorted_by(a,b):
if a[0] == b[0]:
return a[1] - b[1]
else:
return a[0] - b[0]
cmp = functools.cmp_to_key(sorted_by)
a = [[2,0],[1,123],[1,24242],[1,0],[3,0]]
a.sort(key = cmp)
# a = [[1, 0], [1, 123], [1, 24242], [2, 0], [3, 0]]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment