-
-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow sorting NaN values #2
Comments
What is the current behavior? |
It currently throws |
What is the reasoning for it being considered lowest rather than highest? |
NaN means not a number which means nothing which means 0. |
Well but 0 is not the lowest possible number 😅 |
0 is the smallest natural number and is usually a fallback for |
What do you think should be the expected result for |
|
So you're treating My opinion is that this is arbitrary and often not intended. How about, instead, adding an option? const numberSort = require('num-sort');
console.log([1, NaN, -4, 3].sort(numberSort.ascending({ treatNanAs: -Infinity })));
//=> [NaN, -4, 1, 3] |
I agree, |
Infinity is technically a number. NaN is not, hence it should be first/last. |
@sindresorhus Do you prefer a breaking change (simply change the existing behavior) by arbitrarily choosing between first/last, or do you think an option such as I suggested is possible? |
I prefer a breaking change. |
@sindresorhus One last confirmation: [1, -1, -Infinity, NaN].sort(numberSort.ascending)
//=> [NaN, -Infinity, -1, 1]
[1, -1, -Infinity, NaN].sort(numberSort.descending)
//=> [1, -1, -Infinity, NaN] Is this correct? If yes I'll submit a PR |
Correct |
When sorting NaN values, they should be treated as the lowest value in the entire array:
cc @sindresorhus
The text was updated successfully, but these errors were encountered: