I might be missing something obvious here, but why does Python 2.x sorted() function work like this?
In [95]: l = [1, '5', 4, 2, '3', 'b', 'c', '-a']
In [96]: sorted(l)
Out[96]: [1, 2, 4, '-a', '3', '5', 'b', 'c']
Python 3 returns exception (TypeError: unorderable types: str() < int()
), which I expect more than this result.
1 Answer 1
It looks like the ASCII values of the characters are used to sort relative to the actual numbers.
After converting the characters to their decimal ASCII values, the sorted list looks something like this:
[1, 2, 4, '-a', '3', '5', 'b', 'c']
[1, 2, 4, 45 97, 51, 53, 98, 99]
Note that '-a'
is sorted by the first character first, as is normal for an "alphabetical" ordering.
-
So they are silently converted to the textual representation of their numerical character encodings. What a wonderful demonstration that indeed, explicit is better than implicit.Ryan Reich– Ryan Reich2015年11月04日 22:04:23 +00:00Commented Nov 4, 2015 at 22:04
-
2No, there's no silent conversion going on. The numbers all compare less than the strings, so we get all the numbers (in sorted order), followed by all the strings (in their usual lexicographic order based on the underlying ASCII encoding). If you try adding the number
999
to the list (for example), you'll see that in the sorted output it'll end up after all the other numbers, and before all the strings.Mark Dickinson– Mark Dickinson2015年11月06日 08:36:23 +00:00Commented Nov 6, 2015 at 8:36 -
For more info, see: stackoverflow.com/questions/3270680/…Mark Dickinson– Mark Dickinson2015年11月06日 08:41:16 +00:00Commented Nov 6, 2015 at 8:41