[Zope] dtml-in sort by generic function
Oleg Broytmann
Oleg Broytmann <phd@mail2.phd.pp.ru>
Thu, 15 Mar 2001 15:24:09 +0300 (MSK)
On Wed, 14 Mar 2001, Dieter Maurer wrote:
> I am not yet clear about a good syntax. It
> should be something, that pairs the attribute with the sorting
> function essential for this attribute: something like:
>
> sort="attr1[cmp1],attr2,attr3[cmp3],...."
Looks good, although a bit too complex.
> This should mean, use "cmp1" for "attr1", the default comparison
> for "attr2" (i.e. cmp) and "cmp3" for "attr3" (and so on).
>
> Each "cmp" is looked up in the namespace and if not found there,
> in a standard set of comparison function, such that it is
> easy to get "locale_asc", "locale_desc", "case_insensitive_asc", ....
I prefer not to have more specail keywords. Better, let's predefine some
functions (case_insensitive_asc, case_insensitive_desc), and some functions
aonly if locale is already imported (sys.modules.has_key("locale")).
> This way, you get a tuple of comparison functions of the same
> length as the tuple of values.
>
> Now we define:
> def lexicographicCompare(values1,values2,functions):
> '''*values1*, *values2* and *functions* are all tuples of the
> same length with "functions[i]" capable of comparing
> "value1[i]" and "values2[i]".
> The result is the lexicographic comparison of the tuples.'''
> for v1,v2,cmp in map(None,value1,value2,functions):
> c= cmp(v1,v2) # maybe we should do something, if this raises an exception
> if c: return c
> return 0
That's quite good!
Oleg.
----
Oleg Broytmann http://www.zope.org/Members/phd/ phd@phd.pp.ru
Programmers don't die, they just GOSUB without RETURN.