Newbie: sort list of dictionaries

Paul Rubin http
Tue Dec 30 18:59:49 EST 2003


wade at lightlink.com (Wade Leftwich) writes:
> D'oh! That sortlist() function was needlessly clever. This is nicer:
> 
> >>> def sortlist(L, decorator=None):
> ... 	if decorator is None:
> ... 		L.sort()
> ... 		return
> ... 	newL = [(decorator(x), x) for x in L]
> ... 	newL.sort()
> ... 	L[:] = [y for (x,y) in newL]

Ugh!  And don't forget that you still have to write the parametrized
decorator function which is missing there.  It's simpler in this
example to use use a tailored comparison function than to mess with
that DSU approach.




More information about the Python-list mailing list