fixed memory corruption caused by std::sort of NaN by adding checks and double-checks