In Python 3 the ordering comparison operators (<, <=, >=, >) raise a TypeError exception when the operands don’t have a meaningful natural ordering
This change in comparison creates difficulties to order a list of tuples, if there exist None values.
In Python 2:
>>> unordered_list = [('3', '1', None), ('3','1', '4'), ('3', '1', None)]
>>> sorted(unordered_list, reverse=True)
[('3', '1', '4'), ('3', '1', None), ('3', '1', None)]
In Python 3:
>>> unordered_list = [('3', '1', None), ('3','1', '4'), ('3', '1', None)]
>>> sorted(unordered_list, reverse=True)
TypeError: unorderable types: str() < NoneType()
Do you have any idea how to have the same behavior with Python 2 an elegant way?
NOTE: In the above examples I have integers, but it's only an example. The elements of the tuples will have the same type and they could be any type.
ex.2 [('test3','test1', 'test4'), ('test3', 'test1', None)]
ex.3 [( 3, 1, 4), (3, 1, None)]
ex.4 [( 3.1, 1.1, 4.1), (3.1, 1.1, None)]