Sort mixed data



examples/pitfalls/sort.py
from __future__ import print_function

mixed = [10, '1 foo', 42, '4 bar']
print(mixed)  # [100, 'foo', 42, 'bar']
mixed.sort()
print(mixed)  # [42, 100, 'bar', 'foo']

In Python 2 it "works" is some strange way.


$ python examples/pitfalls/sort.py


[10, '1 foo', 42, '4 bar']
[10, 42, '1 foo', '4 bar']

In Python 3 in correctly throws an exception.


air:python gabor$ python3  examples/pitfalls/sort.py


[10, '1 foo', 42, '4 bar']
Traceback (most recent call last):
  File "examples/pitfalls/sort.py", line 5, in <module>
    mixed.sort()
TypeError: unorderable types: str() < int()