[Zope] Sort DateTime objects?

Jim Kutter zope-list@ebizq.net
Thu, 7 Nov 2002 14:43:04 -0500


Hi

I have a product with a class that contains a DateTime object with the name
datetime (I know, bad choice, but the choice has been made...)

So I want to sort a collection of these objects by ascending date.  So I get
all objects of that type into a list called "events" then use the sequence
module

So this is my basic logic -
sorted = sequence.sort(events, (('datetime.x()', 'cmp', 'asc'),))

Where x has been timeTime, millis, and just the datetime attribute itself
('datetime')

This yields the "sorted" list having the event objects in order of their
filename, not datetime value.

The datetime attibute stores both date and time, not just dates - other
objects that just store dates (ie 10/31/2002) work fine with this method.

Implementing my own sort algo with my own cmp function yields correct
results, but is much slower.

def mycmp(a, b):
 if DateTime(a.datetime).millis() < DateTime(b.datetime).millis():
  return 1
 elif DateTime(a.datetime).millis() > DateTime(b.datetime).millis():
  return -1
 else:
  return 0

mysort(events, mycmp)

where mysort is a stock quicksort implementation.

What am I doing wrong with the sequence module?

-jim