define a list sort on the fly in python using lambda and cmp
October 26th, 2008 by Lawrence David
to custom sort a list on the fly, try using the lambda function to do a quick definition of the cmp function. for instance, to sort the list ‘ids’, using each ‘id’ value in an ancillary dictionary ‘id_dict’:
ids.sort(cmp=lambda a,b:id_dict[b] – id_dict[a])