Take the 2-minute tour ×  Hi: I'm trying to sort a list of
tuples in a custom way: For example:  lt = [(2,4), (4,5), (5,2)]
must be sorted:  lt = [(5,2), (2,4), (4,5)]  * b tuple is
greater than a tuple if a[1] == b[0] * a tuple is greater than b
tuple if a[0] == b[1]  I've implemented a cmp function like
this:  def tcmp(a, b):     if a[1] == b[0]:        return -1
elif a[0] == b[1]:        return 1        return 0  but sorting
the list:   lt show me:  lt = [(2, 4), (4, 5), (5, 2)]  What am
I doing wrong?  share|improve this question I see nothing wrong,
except that given your input and comparison function, your
initial list is already sorted. –  Ignacio Vazquez-Abrams Dec 29
'10 at 12:38 ... And what if a[1] == b[0] AND a[0] == b[1]? What
if neither is the case? –  Karl Knechtel Dec 29 '10 at 13:12 add
comment  6 Answers  up vote 2 down vote accepted  I'm not sure
your comparison function is a valid one in a mathematical sense,
i.e. transitive. Given a, b, c a comparison function saying that
a > b and b