Sorting of list containing tuples

Wojciech Muła wojciech_mula at poczta.null.onet.pl.invalid
Thu May 18 21:45:18 CEST 2006


Ronny Mandal wrote:
> 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?

def cmpfun(a,b):
	return cmp(b[1],a[1])

l.sort(cmpfun)



More information about the Python-list mailing list