sort(): Giving third argument?

Thomas Guettler zopestoller at thomas-guettler.de
Fri Mar 21 10:54:08 CET 2003


Hi!

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

ids=[1, 2, 3, 4]

names={
  1: "foo",
  2: "bar",
  ...}

I can't do the following:

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

ids.sort(mycmp)

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)


  thomas





More information about the Python-list mailing list