On Thu, 18 May 2006 21:29:59 +0200 in comp.lang.python, Ronny Mandal <[EMAIL PROTECTED]> wrote:
>Hi! > >Assume we have a list l, containing tuples t1,t2... > >i.e. l = [(2,3),(3,2),(6,5)] > >And now I want to sort l reverse by the second element in the tuple, >i.e the result should ideally be: > > l = [(6,5),(2,3),(3,2)] > > >Any ideas of how to accomplish this? > > >Thanks, > >Ronny Mandal >>> def my_cmp(t1,t2): c1 = t1[1] c2 = t2[1] if c1 > c2: return 1 if c2 > c1: return -1 return 0 >>> l [(2, 3), (3, 2), (6, 5)] >>> l.sort(cmp=my_cmp, reverse = True) >>> l [(6, 5), (2, 3), (3, 2)] HTH, -=Dave -- Change is inevitable, progress is not. -- http://mail.python.org/mailman/listinfo/python-list