The previous answers are helpful, but may not be clear as to the root of the problem.
In any language, sorting applies the specified ordering, defined by the comparison function or in some other way, over the domain of input values. For example, less than, aka operator <, can be used everywhere if and only if less than the appropriate order over the input values.
But this is especially NOT true for floating point values ββand less: "NaN is disordered: it is not equal, more or less than everything, including itself." ( Clear prose from the GNU C manual , but applies to all modern IEEE754 floating point )
Thus, possible solutions:
- first remove NaN by making a correctly defined input domain via <(or another sorting function used)
- define a custom comparison function (aka predicate) that does determine the order for NaN, for example, less than any number or more than any number.
Any approach can be used in any language.
In practice, given python, I would prefer to remove NaNs, if you care, about maximum performance, or if removing NaNs is the desired behavior in context.
Otherwise, you can use the appropriate predicate function via "cmp" in older versions of python or through this and functools.cmp_to_key() . The latter, of course, is a bit uncomfortable than removing NaNs in the first place. When defining this predicate function, care must be taken to avoid performance degradation.
Bob Davis Aug 23 '11 at 17:35 2011-08-23 17:35
source share