sort(): Giving third argument?

Thomas Guettler zopestoller at
Fri Mar 21 10:54:08 CET 2003


If I have a list of IDs and a hash which maps
each ID to a name:

ids=[1, 2, 3, 4]

  1: "foo",
  2: "bar",

I can't do the following:

def mycmp(a, b):
	return cmp(names[a], names[b])


since "name" is unkown in mycmp.

What's the best solution?

What about giving the sort function a third argument?

def mycmp(a, b, names):
	return cmp(names[a], names[b])
ids.sort(mycmp, names)


More information about the Python-list mailing list