[Zope] sort in python script dynamically?

Andre Meyer meyer at acm.org
Tue Jan 13 03:19:01 EST 2004


Let me get back to a related issue regarding sorting, if you permit.


As we learned in previous mails, it is simple to sort a list by any of its
fields if they can be indexed like this:

def sort_by_index(self, list, i):
      list.sort(lambda a, b: cmp(a[i], b[i]))

list = [[x, y], [y, z]]
sort_by_index(list)


But how can one sort by field names (= attributes of an object) as
parameters??? The following certainly does not work!

def sort_by_field(self, list, f):
      list.sort(lambda a, b: cmp(a.f, b.f))

x = myObject(i, j, k)
y = myObject(l, m, n)
list = [x, y]
sort_by_field(list)

How can an attribute name (a string) be translated in the actual attribute?


thanks for your help
André




More information about the Zope mailing list