[Zope] Sorting Zope objects in python scripts

complaw@hal-pc.org complaw@hal-pc.org
Tue, 4 Dec 2001 19:16:52 GMT


> 
> Hi Ron,
> it's not clear to me if you want to dynamically give the property on 
> which to sort or if this can be static. 

Oliver:

As it turned out, it was a static property (attribute).

> I don't know how to do the first 
> in an elegant way, but here is the second:
> 
> def compare_integers(a,b):
>      return cmp(a.element_number, b.element_number)
> 
> myList.sort(compare_integers)

That did the trick.  

Your help is greatly appreciated.

Thanks!

Ron

> 
> i.e. you handle the objects to your custom compare method and it gets 
> the attribute it wants to compare from the objects.
> 
> Oh, and if the object itself knows on what it wants to be sorted, you 
> could use
> 
> def compare_integers(a,b):
>      return cmp(getattr(a,a.what_to_sort), getattr(b,b.what_to_sort))
> 
> i.e. a.what_to_sort would be a string property holding the name of the 
> property on which a and b want to be sorted. You could also use some 
> global variable which you set in your script, and your compare method 
> would retrive it.
> 
> 
> 
> HTH,
> oliver
> 
> 
> 
> 
> 
> 
> _______________________________________________
> Zope maillist  -  Zope@zope.org
> http://lists.zope.org/mailman/listinfo/zope
> **   No cross posts or HTML encoding!  **
> (Related lists - 
>  http://lists.zope.org/mailman/listinfo/zope-announce
>  http://lists.zope.org/mailman/listinfo/zope-dev )
>