[Zope-CMF] Complex sorting on Zcatalogs.

Tres Seaver tseaver@zope.com
Mon, 22 Oct 2001 20:51:12 -0400


Josef Albert Meile wrote:

> Sorry for this question which for some of you could be very simple, 
> but I tried to find the answer on the Zope's website by giving the 
> keywords: ZCatalog, sorting and sort, but I found nothing. I just 
> found that it isn't possible with the original "dtml-in" tag and that 
> there is a testing patch which permits to do it. I guess that it 
> isn't possible with ZCatalogs. Anyway, correct me if I am wrong.
> 
> I would like to make search on a Zcatalog and sort it on two or more 
> field indexes. For example:
> 
> objects=catalog(
>           {
>            'meta_type'       : pType,
>            'review_state'    : pStatus,
>            'sort_on'         : ['personTitle','personName'],
>           }
>         )
> 
> I tried it but it didn't work.
> What is the correct sintax?


Unlike the in-tag's sort attribute, which can be a tuple, the catalog's
'sort_on' parameter must map to an index name (the only reason to have
the catalog sort your requests is to take advantage of indexing).

You might either

   - fetch the objects without sorting, and then sort yourself::

      objects = catalog( ...)
      objects.sort( lambda x, y: cmp( ( x.personTitle(), x.personName() ),
                                      ( y.personTitle(), y.personName() ) )

or:

   - create a PythonScript, 'personTitleName', which concatenates the
     two fields::

       ## Script (Python)  "personTitleName"
       return '%s %s' % ( context.personTitle(), context.personName() )

     and then create a field index on 'personTitleName';  you could then
     query that index using 'sort_on' (after reindexing your content)::

       objects=catalog( { 'meta_type'       : pType
                        , 'review_state'    : pStatus
                        , 'sort_on'         : 'personTitleName'
                        }
                       )

BTW, this is really a question for zope@zope.org, rather than zope-cmf.

Tres.
-- 
===============================================================
Tres Seaver                                tseaver@zope.com
Zope Corporation      "Zope Dealers"       http://www.zope.com