[Zope] Sorting a list of folders based upon PART of the folder id

Max M maxm@mxm.dk
Tue, 17 Jun 2003 23:21:22 +0200


mclinden@informed.net wrote:
> I have an application where a front-end constrains the ids of created 
> folders so that they have some significance to the application. I'd like 
> to be able to sort these folders using Python's sort facility, but I am 
> having problems doing this within a Zope Page Template.


Put it into a python script. Much easier, and an allround good habbit.


> As an example, I would like to be able to do an alphabetic sort of the 
> folder IDs based upon the concatenation of the last and first characters 
> of the idea. I suppose that I could create a Python module to do this, but 
> I was hoping for a slick way to do it entirely within Zope. 

You can use a python script for sorting:

zope_objects.sort(
     lambda x,y: cmp(
         (x.getId()[0], x.getId()[-1]), (y.getId()[0], y.getId()[-1])
     )
)

But it isn't that nice.

The dsu method is most likely both faster and easier to read:

decorated = [(o.getId()[0], o.getId()[-1]) for o in zope_objects]
decorated.sort()
sorted_objects = [d[-1] for d in decorated]

> Also, has anyone written a SQL-like ORDER-BY or GROUP-BY sort routine for 
> Zope/Python?

You can just do a sort on any attribute(s).

zope_objects.sort(lambda x,y: cmp(x.meta_type, y.meta_type))


regards Max M