[Zope] Sorting Zope objects in python scripts

complaw@hal-pc.org complaw@hal-pc.org
Tue, 4 Dec 2001 17:05:17 GMT


If one were to write...

myList = []
for obj in context.objectValues(['Folder', 'DTML Document']):
  if obj.instance_type = 'Desired Class':
    myList.append(obj)

.. then I would want to sort the various objects.  The question is, how do I do
that?

I could do...

myList.sort()

.. but that doesn't give me a way to control on which property the sort is
done.  You could use a sort function, such as...

def compare_integers(first=0, second=0):
  if first > second:
    return 1
  else:
    if first == second:
      return 0
    else:
      return -1

.. and this leads to the part that I just don't grok.  How do I specify a
particular property within an object (such as element_number) as the parameter
to be used for the comparison?  Do I just...

myList.sort(compare_integers)

??

Can anyone give me a quick hint?  The python documentation is a little short in
this area.

Thanks in advance,

Ron