I am trying to sort a list of objects using the date attribute with
list_of_objects.sort(key=lambda x: x.date, reverse=True)
but some dates are just None, which means that I get the error
TypeError: can't compare datetime.datetime to NoneType
is there a way to account for this? e.g. Have objects with date == None
at the top or bottom of the sorted list—or do I need to do this manually?
If you want to create a new sorted list without modifying the original one, you should use the sorted function instead. As you can notice, both sort and sorted sort items in an ascending order by default.
You can sort a one-level bulleted or numbered list so the text appears in ascending (A to Z) or descending (Z to A) alphabetical order. Select the list you want to sort. Go to Home > Sort. Set Sort by to Paragraphs and Text.
list. sort() sorts the list and replaces the original list, whereas sorted(list) returns a sorted copy of the list, without changing the original list.
sort() only works for sorting lists. sorted() function is more versatile as we can use it to sort other data types and objects. Today we will see how to sort lists, tuples and dictionaries using the sorted() function.
You want to sort based on two properties:
You can express your intent in a straightforward manner by sorting on a tuple, where
None
, andlist_of_objects.sort(key=lambda x: (x.date is None, x.date), reverse=True)
This approach circumvents the type error that you are getting, because comparison between tuples is performed left-to-right lazily. The second tuple elements don't get compared unless the first elements are equal.
Here are some examples demonstrating the concept:
>>> xs = [None, 1, 3, None, 2]
>>> sorted(xs, key=lambda x: (x is None, x))
[1, 2, 3, None, None]
>>> sorted(xs, key=lambda x: (x is not None, x))
[None, None, 1, 2, 3]
>>> sorted(xs, key=lambda x: (x is None, x), reverse=True)
[None, None, 3, 2, 1]
>>> sorted(xs, key=lambda x: (x is not None, x), reverse=True)
[3, 2, 1, None, None]
You can modify your lambda slightly:
from datetime import datetime
list_of_objects.sort(key=lambda x: x.date or datetime.min, reverse=True)
If they're appearing at the wrong end of the sort, use datetime.max
instead.
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