Multiple "cmp"s chained one after another

Peter Hansen peter at engcorp.com
Sat May 14 10:27:21 EDT 2005


vincent wehren wrote:
> "Volker Grabsch" <volker_grabsch at v.notjusthosting.com> schrieb im 
> Newsbeitrag news:d64io9$s6o$05$1 at news.t-online.com...
> | However, I don't want to sort them the default way. These are birthdays,
> | so only the month and day do matter, not the year. E.g.:
> |
...
> If you don't care about the year, why not just "normalize" the year
> to all be the same using the replace method of the date instance?
> Something like:
> 
> d1 = datetime.date(2004, 12, 2)
> d2 = datetime.date(2001, 12, 3)
> d3 = datetime.date(2002, 12, 6)
> d4 = datetime.date(1977, 12, 7)
> dates =[d1,d2,d3,d4]
> datesNorm = [obj.replace(year=1900) for obj in (dates)]
> datesNorm.sort()
> print datesNorm # etcetera

Or just use the .timetuple() method on datetime objects and sort on the 
8th element of the 9-element tuple, which is the day-of-the-year.

-Peter



More information about the Python-list mailing list