[Zope] Help w/ Date Sorting

Dylan Reinhardt zope@dylanreinhardt.com
Wed, 05 Feb 2003 08:21:05 -0800


At 07:24 AM 2/5/2003, JBeard@porternovelli.com wrote:
>What my dateline field is:
>A property: type = string. They look like this "01-25-2003" and so on....

Well... they're sorting correctly for strings, aren't they?  01-24-2003 
comes before 01-31-2002 alphabetically.

You'll probably need to do one of the following:
1. Don't store dateline as a string, store it as a float (if you care about 
time) or long (if you don't) and format it to a string *after* you've done 
your sort.
2. Format the date as yyyy-mm-dd
3. Devise something to parse dateline and return something that can be 
correctly sorted, as above.  Sort by that but present the original to the user.

I find it's quite helpful to have external methods that provide 
time.strftime() and time.strptime().  You'll probably want to set yourself 
up with these, get comfortable with them and then it will probably be 
obvious which solution fits best with your other requirements.

If you're running Windoze, you may need to provide strptime on your 
own.  One version of it is here:
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/56036


HTH,

Dylan