[Zope] tal:repeat

Joel Burton joel@joelburton.com
Wed, 27 Feb 2002 09:40:50 -0500 (EST)


On Tue, 26 Feb 2002, Daniel Meier wrote:

> hi there
>
> do you know haow I can sort with tal:repeat
>
> I have the following statement and I'd like to sort
> them alphabetically:
>
> <tr tal:repeat="obj python: here.objectValues()">

[fyi to TALES newbies: you could also write the above line as
 tal:repeat="obj here/objectValues"]


Zope Book, 2.5 version gives a good example of sorting on p137.
Essentially, you can use the sequence.sort method or write your
own tiny sort routine. These are both good solutions for medium and hard
cases.

For simple cases (sort a list on items in it):

<tr tal:define="objects here/objectValues;
                junk python:objects.sort();"
    tal:repeat="obj objects">

The fact that python never added a sort() function that returns
the list in sorted mode (rather than sorting in-place) is annoying to me.
Arguably, there are good reasons to sort in-place (memory), but in most
cases, it's useful to have both.

In a External Method called "Utils", I have a tiny tiny routine called
rsort which is simply

def rsort(l):
  "Return a list, sorted"
  l.sort()
  return l

So the example above could then just be:

<tr tal:repeat="obj python: here.objectValues().rsort()">


-- 

Joel BURTON  |  joel@joelburton.com  |  joelburton.com  |  aim: wjoelburton
Independent Knowledge Management Consultant