sort(cmp=func)

Tobiah toby at tobiah.org
Wed Jul 9 17:27:10 EDT 2008


I have a list of objects that generate code.  Some
of them depend on others being listed first, to 
satisfy dependencies of others.

I wrote a cmp function something like this:

def dep_cmp(ob1, ob2):
	
	if ob1.name in ob2.deps:
		return -1
	else:
		return 1

I also tried returning 0 when there were no dependency
relationships between the objects.

This failed, because every object was not compared with
ever other.  I imagine that this is because sort assumes
that if a > b and b > c, then a > c.  In my case, this
isn't really true.  If a is not dependent on b, and
b is not dependent on c, that doesn't mean that a is not
dependent on c.

Is there a better way?

Thanks

Tobiah
** Posted from http://www.teranews.com **



More information about the Python-list mailing list