Hi: I'm trying to sort the list of tuples in my own way: For example:
lt = [(2,4), (4,5), (5,2)]
need to sort:
lt = [(5,2), (2,4), (4,5)]
Rules:
* b tuple is larger than a tuple if a [1] == b [0]
* a tuple is greater than b tuple if a [0] == b [1]
I implemented the cmp function as follows:
def tcmp(a, b): if a[1] == b[0]: return -1 elif a[0] == b[1]: return 1 else: return 0
but sort the list:
lt.sort(tcmp)
lt show me:
lt = [(2, 4), (4, 5), (5, 2)]
What am I doing wrong?
source share