Skip to content

Instantly share code, notes, and snippets.

@huklee
Created May 30, 2018 00:49

Revisions

  1. huklee created this gist May 30, 2018.
    13 changes: 13 additions & 0 deletions sort_2key.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,13 @@
    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]]