[Zope-CMF] sorting lists...
Antony Higgs
ahiggs@brookes.ac.uk
Mon, 31 Mar 2003 14:28:00 +0100
Tres -> thanks, it works a treat!
I have been trying to make it possible to use this same function to alpha
sort different lists by different properties - not always by 'Title'.
--------------
##parameters=pType,alphaBy
objects = context.contentValues(filter={'portal_type':pType})
tried...
objects.sort( lambda x, y: cmp( x.alphaBy, y.alphaBy ) )
...and then...
objects.sort( lambda x, y: cmp( x[alphaBy], y[alphaBy] ) )
return objects
--------------
any thoughts anyone?
thanks
Antony
Tres Seaver wrote:
> On Fri, 2003-03-28 at 04:46, Antony Higgs wrote:
> > Hi
> >
> > Using contentValues() to return a list of content, I am left with the
> > following...
> >
> > [ <Document at /my_folder/doc_a_id>,
> > <Document at /my_folder/doc_b_id>,
> > <Document at /my_folder/doc_c_id> ]
> >
> > Is it possible to sort the list alphabetically on an attribute of each
> > Document. ie. on 'Title'?
> >
> > ...and if so, how? ;-)
>
> The naive implementation would be:
>
> objects = context.contentValues()
> objects.sort( lambda x, y: cmp( x.Title(), y.Title() ) )
> return objects
>
> For a really long list, you may want to create a "decorated"
> intermediate (see the Python Cookbook for rationale):
>
> objects = [ ( x.Title(), x ) for x in context.contentValues() ]
> objects.sort() # tuples compare "lexicographically"
> return [ x[1] for x in objects ]
>
> Tres.
> --
> ===============================================================
> Tres Seaver tseaver@zope.com
> Zope Corporation "Zope Dealers" http://www.zope.com
>
> _______________________________________________
> Zope-CMF maillist - Zope-CMF@zope.org
> http://mail.zope.org/mailman/listinfo/zope-cmf
>
> See http://collector.zope.org/CMF for bug reports and feature requests
--
--------------------------
Antony Higgs
Web Editor/Administrator
Creative Services
Oxford Brookes University
Oxford. OX3 OBR
t: +44 (0) 1865 484459
f: +44 (0) 1865 484456
w: www.brookes.ac.uk
--------------------------