NaN is not one bit pattern: sorting floats by integer keys
Floats · sorting · NaN
float("nan") and 0/0 both print as nan, but they are not always the same bits. A sort that never compares the floats, and instead ranks an integer key made from those bits, can give those NaNs a rank against each other. That breaks a stable rule: equal values keep the earlier column. Fold every NaN to one bit pattern before the key is built.