-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
Comparator edge-case handling #1768
Comments
normally
That's not really the case with _.sortBy([1, 2, NaN, {}, NaN, undefined]);
// => [1, 2, NaN, {}, NaN, undefined] As for _.max([NaN, {}]); // -Infinity Is fine as I found the one valid case was to adjust Other than that it looks like you should be filtering your values to avoid headaches with |
I don't think thats a good argument seeing _.sortBy([1, 2, NaN, {}, NaN, undefined, 1]);
// => [1, 2, NaN, {}, NaN, 1, undefined] |
I'm saying your statement is incorrect about In ES6, if the
|
Sorting non-comparable or undefined values is generally undefined behavior — and I don't think it really matters what Underscore happens to do when it occurs. But that said, if there's a change that makes things internally consistent, and that makes the codebase simpler and not more complicated, that sounds fine. |
I'm thinking a comparator like this should be used internally. Let me know if I should work on a pr // Default internal comparitor for determining whether a is greater (1),
// equal (0) or less than (-1) some object b
var internalComparator = function(a, b) {
if (a === b) return 0;
var isANaN = isNaN(a), isBNaN = isNaN(b);
if (isANaN || isBNaN) {
if (isANaN && !isBNaN) return -1;
if (isBNaN && !isANaN) return 1;
return a > b ? 1 : (b > a) ? -1 : 0;
}
return a > b ? 1 : -1;
}; |
What's the comparator do (example input/output)? If that's native |
Yeah, again I'm arguing On Fri, Aug 29, 2014 at 11:39 AM, John-David Dalton <
|
Some examples // examples
internalComparitor({}, 'a'); // => -1
internalComparitor('a', {}); // => 1
internalComparitor(1, {}); // => 1
internalComparitor({}, 1); // => -1
internalComparitor(1, void 0); // => 1
internalComparitor(void 0, 1); // => -1
internalComparitor(2, 1); // => 1
internalComparitor(1, 2); // => -1 |
I meant input/output of |
I'd like issue to change the behaviour below _.sortBy([{}, {a: 1}, {a: 2}, {a: 3}], 'a');
// => [{}, {a: 1}, {a: 2}, {a: 3}]
// instead of => [{a: 1}, {a: 2}, {a: 3}, {}]
Yeah, I don't care either way for this case, I just want undefined to be like NaN at the start of the result. |
I'm cool with that. |
This may look a bit funny, but for grouping var internalComparator = function(a, b) {
var isANaN = isNaN(a), isBNaN = isNaN(b);
if (isANaN || isBNaN) {
if (isANaN && !isBNaN) return -1;
if (isBNaN && !isANaN) return 1;
return a > b ? 1 : (b > a) ? -1 : (a == void 0) - (b == void 0);
}
return a == b ? 0 : a > b ? 1 : -1;
};
internalComparator(void 0, NaN); // => 1
internalComparator(NaN, void 0); // => -1
internalComparator(void 0, void 0); // => 0 This will place |
As I mentioned in a pull request on backbone, it might be worth adding this comparison function to the underscore api, at least as a "mostly internal" function, like iteratee. Both because this might genuinely be useful to users, and because it would play nicely with that pull request - which is, itself, an example of how this comparison function might be useful to users :) |
|
There are several inconsistencies with the comparator functions I feel should be addressed in 2.0. Mentioned this earlier in jashkenas/backbone#3243
max
:NaN
includingundefined
is considered the lowest item in the array. Even lower then no itemmin
: same as max butNaN
values are considered the highest itemssortBy
: special cases allundefined
values to the end of the array. AllNaN
values are moved to the front of the arraysortedIndex
: different fromsortBy
(see lodash/lodash@b07337f#commitcomment-7164123): computedNaN
is moved to the end of the list instead of the frontI propose a
baseComparator(a, b)
used by those 4 functions (special case min?) which handles these cases consistently. I feel strongly about changingsortBy
's handling ofNaN
vsundefined
. I think they should be consideredNaN < -Infinity < x
.The text was updated successfully, but these errors were encountered: