[Zope-CMF] Need advice on sorting my custom CMF object
Dieter Maurer
dieter@handshake.de
Fri, 23 Nov 2001 19:31:34 +0100
Lynn Walton writes:
> ....
> My question is in regard to sorting. I want to sort on the postdate
> which will be a DateTime() object. I don't know what my options are and
> what is "better". Trying to add an index to the portal catalog for my
> CMF type and it's postdate attribute? If so, I'm not sure how to do
> that. I don't anticipate there ever being more than 20 Job Postings in
> each of the three folders so I'm guessing it isn't worth doing it with
> the portal catalog. I read somewhere where zpt's don't have the sort
> feature that dtml-in does.
When your result list could be large (and you were using batching),
then it would definitely be better to delegate the sorting to
the catalog.
But as your result lists are small, this may not be necessary
(though probably still the easiest solution).
I see three options:
1. index your attribute in "portal_catalog"
go to the "Indexes" tab of "portal_catalog" and
add a "FieldIndex" with your attribute's name
Use the "sort_on" parameter of the catalog's search
method to let it sort
2. Sort with the versatile "sequence.sort" function.
You find a description in ".../DocumentTemplate/sequence/SortEx.py".
There a two variants:
a) define your attribute as a Metadata element (-->
"Metadata" tab) of "portal_catalog".
Then, during cataloging, the value is stored
in the catalog and available in the result
rows for catalog queries.
You do not need to provide your own
comparison function but can simply tell
"sequence.sort" to sort on this attribute.
b) you do not modify "portal_catalog".
Then the result returned by a search does not
know about your attribute. It is necessary
to fetch the object (---> getObject) and
ask it for its attribute value.
This requires either a specific comparison function
or preprocessing the query result (calling
"getobject" on each of them and sorting the resulting
list).
Dieter