[Zope] dtml-in sort by generic function
Oleg Broytmann
Oleg Broytmann <phd@mail2.phd.pp.ru>
Wed, 14 Mar 2001 12:34:58 +0300 (MSK)
Hi!
Thank you for stepping in!
On Tue, 13 Mar 2001, Dieter Maurer wrote:
> > but a colleague of mine, Dennis Otkidach suggested more general approach -
> > to make a patch that will allow to pass a generic function to sort(),
> A very good idea!
>
> However, you should note:
>
> * "dtml-in" does not sort single values, but tuples
> ( sort_values, client), where "sort_values" is
> either a single value or a list of values and
> "client" is the current element.
>
> Your sort function must handle this case
I know it! From time to time I scan and rescan Zope sources just for
better understanding and finding of hidden treasures (there are many :).
> * "dtml-in" supports multiple sort keys.
> I would expect to have a comparison function for
> each sort key, with the global comparison function
> a composition of that for the sort keys.
>
> Thus, you can combine a locale sensitive sort with
> (e.g.) a date sort.
But of course. Or simulating SQL "ORDER BY date DESC, title ASC" :)
But I do not see a way to do it in generic fashion. I can write a
specific comparison function for every specific case, but how I can combine
few comparison functions into one in a generic way? Imagine a have a string
comparison str_cmp, and date comparison date_cmp functions. Now I need to
pass to dtml-in a combined function. Should it be a Python Script that just
calls str_cmp and date_cmp in turn, something like
result = str_cmp(s1[0], s2[0])
if result: return result # if not equal - order by first key
result = date_cmp(s1[1], s2[1]) # else order by second key
return result
Oleg.
----
Oleg Broytmann http://www.zope.org/Members/phd/ phd@phd.pp.ru
Programmers don't die, they just GOSUB without RETURN.