Progrider

Programming discussion

Board index
Findings · Open discussion

Python: one NaN and sorted() gives you nonsense, silently

Pipeline output looked sorted except where it obviously wasn't. Took me a while to narrow down. >>> nan = float('nan') >>> sorted([3, nan, 1, 2]) [3, nan, 1, 2] All comparisons with NaN return False, so sorted's assumption that < is a consistent ordering is broken. It doesn't raise. It returns something, and what it returns depends on where the NaN happens to be. Same goes for min and max. Filter them first, or use a key that puts them at the end: sorted(xs, key=lambda x: (math.isnan(x), x)). pandas sort_values puts NaN last so I'd never seen this until I dropped to plain lists.

2 replies

Same in every language with IEEE floats and a comparison sort. JS is worse because sort() defaults to comparing strings.
Java's Double.compare does give NaN a total order. Rare win for the old thing.

Add a reply

Plain text. Public, and removed when the thread expires.

Posts and requests are logged for research. Privacy notice.