This is a slight update to my previous question
I have a Python list called results. Most result objects in the results list have a person object, and most person objects have a birthdate property (result.person.birthdate). The birthdate is a datetime object.
I would like to order the list of results by birthdate with the oldest first. However, if there isn't a person object or the person object doesn't have a birthdate I would still like the result included in the list of results. At the end of the list would be ideal.
What is the most Pythonic way to do this?
def birthdate_key(x):
missing = (x.person is None or x.person.birthdate is None)
return (missing, x.person.birthdate if not missing else None)
results.sort(key=birthdate_key)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With