Hi, I would like to use dtml-in's sort functionality, but on indices of lists, not attributes. Basically, I have a function, say getLoL() that generates a list of lists ( e.g. [ [A1,A2,A3],[B1,B2,B3], ...] I can use dtml-in to loop through this like: <dtml-in expr="getLoL()"> First index: <dtml-var expr="_.getitem('sequence-item')[0]"> </dtml-in> How can I now sort based on say the second index of the sequence-item? (ie sort A2, B2, C2, etc) I've looked at the DT_In.py code, and think I need to make modifications to allow for indexing (as opposed to getattr's) but was wondering if there was an existing way. Thanks, Robert ------------------------------------------------------------ Get unlimited dialup access at http://www.spininternet.com/
On Mon, Apr 15, 2002 at 06:10:38PM -0400, ririe@spininternet.com wrote:
I would like to use dtml-in's sort functionality, but on indices of lists, not attributes.
Basically, I have a function, say getLoL() that generates a list of lists ( e.g. [ [A1,A2,A3],[B1,B2,B3], ...]
I can use dtml-in to loop through this like:
<dtml-in expr="getLoL()"> First index: <dtml-var expr="_.getitem('sequence-item')[0]"> </dtml-in>
How can I now sort based on say the second index of the sequence-item? (ie sort A2, B2, C2, etc)
I've looked at the DT_In.py code, and think I need to make modifications to allow for indexing (as opposed to getattr's) but was wondering if there was an existing way.
In Zope 2.5+ all things are already there; source code is in the DocumentTemplate/sequence directory, but it is already attached to dtml-in. Usage (this is general Extended DTML Sorting HOWTO): <dtml-in mysequence sort="sortDesciption"> SortDescription is a list of sort options, separated by comma. Every sort option defines a field to sort upon, sort (comparison) function, and sort direction (ascending/descending); fileds in a sort option separated by slash. Examples: <dtml-in mydocuments sort="date/cmp/desc,title"> <dtml-in mydocuments sort="date/cmp/desc,title/locale"> <dtml-in mydocuments sort="date/cmp/desc,title/locale_nocase"> There are few predefined comparison functions: cmp (standard builtin cmp), nocase (case-insensitive cmp); id Zope had been started with -L locale parameter, there are also locale-aware functions: locale (alias strcoll) and locale_nocase (strcoll_nocase). If predfined functions do not suite one's needs, one can write his/her own function. The entire framework make the writing very simple - you only need to write a cmp-like function that compares two values and return -1, 0 or 1. Unfortunately for you, dtml-in sort may be used only with lists of objects or lists of dictionaries. So I recommend you to write an External Method or Python Script that sorts the list. Something like this: def sortListsBy2ndElement(l1, l2): return cmp(l1[1], l2[1]) def mySort(myListOfLists): myListOfLists.sort(sortListsBy2ndElement) Oleg. -- Oleg Broytmann http://phd.pp.ru/ phd@phd.pp.ru Programmers don't die, they just GOSUB without RETURN.
participants (2)
-
Oleg Broytmann -
ririe@spininternet.com