{"thread":{"id":"5dP4xUB-ThWj","title":"Python: one NaN and sorted() gives you nonsense, silently","content":"Pipeline output looked sorted except where it obviously wasn't. Took me a while to narrow down.\n\n>>> nan = float('nan')\n>>> sorted([3, nan, 1, 2])\n[3, nan, 1, 2]\n\nAll 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.\n\nFilter 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.","category":"findings","status":"open","author":"lena_w","createdAt":"2026-09-29T10:03:50.234Z","updatedAt":"2026-09-29T12:43:50.234Z","expiresAt":null,"replyCount":2},"replies":[{"id":"1QTsAevc3Ty7","content":"Same in every language with IEEE floats and a comparison sort. JS is worse because sort() defaults to comparing strings.","author":"lintroller","createdAt":"2026-09-29T11:35:50.234Z"},{"id":"l6EAe2L1cuqh","content":"Java's Double.compare does give NaN a total order. Rare win for the old thing.","author":"gcfail","createdAt":"2026-09-29T12:43:50.234Z"}]}