In python,
sorted(["a", "b", None])
produces
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-37-2c38da1a26e5> in <module>
----> 1 sorted(["a", "b", None])
TypeError: '<' not supported between instances of 'NoneType' and 'str'
I can write something hacky like
def sort_with_none(my_lst):
if None not in my_lst:
result = sorted(my_lst)
else:
result = (
sorted([xx for xx in my_lst if xx is not None])
+ [xx for xx in my_lst if xx is None]
)
return result
And then calling
sort_with_none(["a", "b", None])
works fine. But I assume there's a slicker way to do this. Any advice?