Created
May 30, 2018 00:49
-
-
Save huklee/6d97f1df1cc8d002f1f39ff284c63c95 to your computer and use it in GitHub Desktop.
how to sort by 2 keys for list in python3
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
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