Sort DateTime objects?
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
Jim Kutter writes:
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'),)) ^^^^^^^^^^^^ This must be the name of an attribute or parameterless method, not the method call itself. Use "x" alone.
Dieter
Thanks, but still no luck. What method name should I provide? I tried datetime.millis, datetime, and datetime.timeTime (all with no (), I was just passing the reference) -jim ----- Original Message ----- From: "Dieter Maurer" <dieter@handshake.de> To: "Jim Kutter" <zope-list@ebizq.net> Cc: <zope@zope.org> Sent: Friday, November 08, 2002 5:11 PM Subject: Re: [Zope] Sort DateTime objects?
Jim Kutter writes:
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'),)) ^^^^^^^^^^^^ This must be the name of an attribute or parameterless method, not the method call itself. Use "x" alone.
Dieter
I've seen this and I would say we have a bug. It seems that when you use a method to sort on, what happens is that the method is not called, it is simply sorting on the reference of the method. Which is almost the same as not sorting at all ;). The reference I'm talking about is the <function a at 00895D74>, string when doing a __repr__ of a method/function. I hope I'm wrong here but I can't find any explanation for the behaviour. Any thoughts? Phil On Mon, 11 Nov 2002 11:19:03 -0500 "Jim Kutter" <zope-list@ebizq.net> wrote:
Thanks, but still no luck. What method name should I provide?
I tried datetime.millis, datetime, and datetime.timeTime (all with no (), I was just passing the reference)
-jim ----- Original Message ----- From: "Dieter Maurer" <dieter@handshake.de> To: "Jim Kutter" <zope-list@ebizq.net> Cc: <zope@zope.org> Sent: Friday, November 08, 2002 5:11 PM Subject: Re: [Zope] Sort DateTime objects?
Jim Kutter writes:
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'),)) ^^^^^^^^^^^^ This must be the name of an attribute or parameterless method, not the method call itself. Use "x" alone.
Dieter
_______________________________________________ Zope maillist - Zope@zope.org http://lists.zope.org/mailman/listinfo/zope ** No cross posts or HTML encoding! ** (Related lists - http://lists.zope.org/mailman/listinfo/zope-announce http://lists.zope.org/mailman/listinfo/zope-dev )
Phil Harris writes:
I've seen this and I would say we have a bug.
It seems that when you use a method to sort on, what happens is that the method is not called, it is simply sorting on the reference of the method. Which is almost the same as not sorting at all ;).
The reference I'm talking about is the <function a at 00895D74>, string when doing a __repr__ of a method/function.
I hope I'm wrong here but I can't find any explanation for the behaviour. You are almost surely right:
Look at "DocumentTemplate/sequence/SortEx.py" near line 102. You should see: if not basic_type(akey): Change this to if not basic_type(type(akey)): If this fixed the problem, please file a "Bug+Solution" report to the Zope collector. Dieter
participants (3)
-
Dieter Maurer -
Jim Kutter -
Phil Harris