On Thu, Mar 05, 2020 at 04:46:14PM -0600, Tim Peters wrote:
[Steven D'Aprano firstname.lastname@example.org]
Sorting doesn't require a total order. Sorting only requires a weak order where the only operator required is the "comes before" operator, or less than. That's precisely how sorting in Python is implemented.
Let's be careful here. Python's list.sort() guarantees that _if_ the elements are totally orderable, it will deliver the unique stable permutation consistent with that ordering. Otherwise, the only thing it guarantees is that the output will be _some_ permutation of the input.
Thank you for the correction, I did know that but didn't express myself as well as I should have. I wasn't intending to imply that sort could magically tease out a stable, intuitive smallest-to-largest order from data that doesn't have a stable, intuitive smallest-to-largest order :-)
All I intended was to say that sort would "work" in the sense you state: it will give *some* permutation of the data, where (I think, correct me if I'm wrong) each element compares less than the following element.
Wait, no, that's not right either... each element doesn't compare less than the previous element?
No need to imagine it, sort already type-checks its arguments:
py> sorted([1, 3, 5, "Hello", 2]) TypeError: '<' not supported between instances of 'str' and 'int'
Yes and no ;-) list.sort() makes no attempt to check element types for the sake of checking them.
Again, mea culpa, I wasn't as clear as I intended. I didn't mean sort itself did explicit type-checks. As you say, it's just passing on the type errors from comparing incomparable types.
Thanks for clarifying what I meant :-)