Following up on this question: Sort when values are None or empty strings python I am looking for a fully working solution in Python 3.
In Python 2.7.6, I can successfully do the following:
> list_with_none = [(1,"foo"), (2,"bar"), (3,""), (4,None), (5,"blub")]
> col = 1
> sorted(list_with_none, key=lambda k: (k[col] is None, k[col] == "", k[col]))
[(2, 'bar'), (5, 'blub'), (1, 'foo'), (3, ''), (4, None)]
> sorted(list_with_none, key=lambda k: (k[col], k[col] is None, k[col] == ""), reverse=True)
[(1, 'foo'), (5, 'blub'), (2, 'bar'), (3, ''), (4, None)]
In Python 3.4.3, I can't find a working solution for reverse sorting, which puts empty strings and None values at the end of the list (which is what I explicitly need):
> list_with_none = [(1,"foo"), (2,"bar"), (3,""), (4,None), (5,"blub")]
> col = 1
> sorted(list_with_none, key=lambda k: (k[col] is None, k[col] == "", k[col]))
[(2, 'bar'), (5, 'blub'), (1, 'foo'), (3, ''), (4, None)]
> sorted(list_with_none, key=lambda k: (k[col], k[col] is None, k[col] == ""), reverse=True)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unorderable types: NoneType() < str()