Am 18.02.2014 08:35, schrieb Greg Ewing: > Tim Peters wrote: >> >> [Greg Ewing] >> >>>often >>>one wants to sort a collection of objects having >>>keys that can take on null values. >> >> Perhaps that's often true of your code, but it's never been true of mine. > > It's fairly common in accounting circles. I have a > collection of invoices, each of which can have a due > date specified, but doesn't have to. I want to sort > the invoices by due date. It's not particularly > important whether the missing dates sort first or > last, but it *is* important that the sort *not blow > up*. > > Dates seem to be a particularly irksome case. Here's > one suggestion from StackOverflow for dealing with > the problem: > > import datetime > mindate = datetime.date(datetime.MINYEAR, 1, 1) > > def getaccountingdate(x): > return x['accountingdate'] or mindate > > results = sorted(data, key=getaccountingdate) > > That's a ridiculous amount of boilerplate for doing > something that ought to be straightforward.
Seeing how you need a key function in any case for this sort to work, it's only the "or mindate" added, which I can't recognize as "ridiculous amount of boilerplate". Much more so since you can put the key function in a shared module and import it from there anywhere you need it. Georg _______________________________________________ Python-Dev mailing list Python-Dev@python.org https://mail.python.org/mailman/listinfo/python-dev Unsubscribe: https://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com