-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
Labels
Description
There are several inconsistencies with the comparator functions I feel should be addressed in 2.0. Mentioned this earlier in jashkenas/backbone#3243
max:NaNincludingundefinedis considered the lowest item in the array. Even lower then no item
_.max([{a: {}}], 'a'); // => -Infinitymin: same as max butNaNvalues are considered the highest itemssortBy: special cases allundefinedvalues to the end of the array. AllNaNvalues are moved to the front of the arraysortedIndex: different fromsortBy(see lodash/lodash@b07337f#commitcomment-7164123): computedNaNis moved to the end of the list instead of the front
var array = [{a: NaN}, {a: NaN}, {a: {}}, {a: 1}, {a: 2}];
_.sortedIndex(array, {a: 3}, 'a'); // => 0
var sorted = _.sortBy(array.concat({a: 3}), 'a');
_.findIndex(sorted, {a: 3}); // => 5I propose a baseComparator(a, b) used by those 4 functions (special case min?) which handles these cases consistently. I feel strongly about changing sortBy's handling of NaN vs undefined. I think they should be considered NaN < -Infinity < x.
// Reasoning
_.sortBy([{a: 1}, {a: -Infinity}, {a: 2}, {}], 'a')
// Should => [{}, {a: -Infinity}, {a: 1}, {a: 2}]