[issue36095] Better NaN sorting.

Mark Dickinson report at bugs.python.org
Sun Dec 15 14:43:12 EST 2019


Mark Dickinson <dickinsm at gmail.com> added the comment:

> IMHO sorting functions should emit a warning if they contains an unorderable objects

How do you define "unorderable", and how do you propose to detect "unorderable objects" efficiently in practice within the sorting algorithm?

What you propose isn't practical in full generality. The case in which `sorted` can't give a well-defined result is the case where the collection of elements being sorted, under the given ordering, is not a total preorder. Being a total preorder is a property of the whole collection being sorted, not of individual objects: it's not necessarily true that if the collection is not totally preordered then there's any one item you can point to as being "unorderable".

Checking for a total preorder is much more expensive than simply sorting (at best it would be a quadratic time post-sort check), so that's not a reasonable check to make.

So by checking for unorderable objects, whatever those are, you're only solving a small part of the overall problem, at the expense of extra computation. You're also likely breaking the existing guarantees (depending on how you do this checking) that `sort` and `sorted` only rely on `<` comparisons; that would be a backwards compatibility break.

----------

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue36095>
_______________________________________


More information about the Python-bugs-list mailing list