[Zope] Sort by content creation date

Casey Duncan casey@zope.com
Wed, 21 Aug 2002 09:34:48 -0400


This sounds like an application design problem. You should not store data=
 in=20
its representative form (ie a string) if there is a native form for the d=
ata.=20
You should instead store the data as date type (assuming it is a property=
,=20
use DateTime objects if not) and format it as desired when you render it.=
=20
Then you can sort it, and change the formatting (or use multiple formats)=
=20
easily.

Now assuming you cannot do this, here is a hack solution to your problem,=
=20
write a python script as follows:

obs =3D []
for kube in context.objectValues('Kube'):
    day, month, year =3D content_creation_date.split('/')
    obs.append((int(year), int(month), int(day), kube.getId()))

obs.sort()
return [context[id] for year, month, day, id in obs]

Here's how it works:

Step through each Kube object, extracting the day month and year from the=
=20
creation date. Then append a tuple with the date information as ints in t=
he=20
proper order for sorting (year, month, day) plus the id of the Kube so we=
 can=20
get it back out at the end. Then sort this list of tuples (The Python sor=
t=20
method does the right thing for us here). The last line uses a list=20
incomprehension to make a new list of Kubes in order by date by pulling t=
he=20
ids out of the tuples and looking them up in the context.

The only hangup would be if content_creation_date is not a string or cont=
ains=20
things other than integers in the date field or is not consistently=20
formatted. If its the latter, you're probably SOL.

If this is too slow, you could use ZCatalog to speed it up, by calculatin=
g a=20
real DateTime from the content_creation_date and indexing against it. The=
n=20
you can sort by the index.

hth,

-Casey
   =20

On Wednesday 21 August 2002 06:24 am, Patrick Romano wrote:
> I want to sort a certain number of Kubes by their content creation date=
,=20
> which is   DD/MM/YY,
> but if I do it like that
>=20
> <dtml-in expr=3D"news.objectValues('Kube')"  sort=3D"content_creation_d=
ate">
>=20
> the Kubes are sorted by date starting from the left : sorted by DD firs=
t.
> How can I achieve to have it soted by YY without changing the order of =
my=20
> date (DD/MM/YY)?
>=20
> Thanks,
> Patrick