mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-09 10:27:49 +00:00
Make null sorted right behind undefined and NaN.
This commit is contained in:
@@ -298,13 +298,22 @@
|
|||||||
*/
|
*/
|
||||||
function baseCompareAscending(value, other) {
|
function baseCompareAscending(value, other) {
|
||||||
if (value !== other) {
|
if (value !== other) {
|
||||||
var valIsReflexive = value === value,
|
var valIsNull = value === null,
|
||||||
|
valIsUndef = value === undefined,
|
||||||
|
valIsReflexive = value === value;
|
||||||
|
|
||||||
|
var othIsNull = other === null,
|
||||||
|
othIsUndef = other === undefined,
|
||||||
othIsReflexive = other === other;
|
othIsReflexive = other === other;
|
||||||
|
|
||||||
if (value > other || !valIsReflexive || (value === undefined && othIsReflexive)) {
|
if ((value > other && !othIsNull) || !valIsReflexive ||
|
||||||
|
(valIsNull && !othIsUndef && othIsReflexive) ||
|
||||||
|
(valIsUndef && othIsReflexive)) {
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
if (value < other || !othIsReflexive || (other === undefined && valIsReflexive)) {
|
if ((value < other && !valIsNull) || !othIsReflexive ||
|
||||||
|
(othIsNull && !valIsUndef && valIsReflexive) ||
|
||||||
|
(othIsUndef && valIsReflexive)) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -3037,7 +3046,7 @@
|
|||||||
var mid = (low + high) >>> 1,
|
var mid = (low + high) >>> 1,
|
||||||
computed = array[mid];
|
computed = array[mid];
|
||||||
|
|
||||||
if (retHighest ? (computed <= value) : (computed < value)) {
|
if ((retHighest ? (computed <= value) : (computed < value)) && computed !== null) {
|
||||||
low = mid + 1;
|
low = mid + 1;
|
||||||
} else {
|
} else {
|
||||||
high = mid;
|
high = mid;
|
||||||
@@ -3067,17 +3076,23 @@
|
|||||||
var low = 0,
|
var low = 0,
|
||||||
high = array ? array.length : 0,
|
high = array ? array.length : 0,
|
||||||
valIsNaN = value !== value,
|
valIsNaN = value !== value,
|
||||||
|
valIsNull = value === null,
|
||||||
valIsUndef = value === undefined;
|
valIsUndef = value === undefined;
|
||||||
|
|
||||||
while (low < high) {
|
while (low < high) {
|
||||||
var mid = floor((low + high) / 2),
|
var mid = floor((low + high) / 2),
|
||||||
computed = iteratee(array[mid]),
|
computed = iteratee(array[mid]),
|
||||||
|
isDef = computed !== undefined,
|
||||||
isReflexive = computed === computed;
|
isReflexive = computed === computed;
|
||||||
|
|
||||||
if (valIsNaN) {
|
if (valIsNaN) {
|
||||||
var setLow = isReflexive || retHighest;
|
var setLow = isReflexive || retHighest;
|
||||||
|
} else if (valIsNull) {
|
||||||
|
setLow = isReflexive && isDef && (retHighest || computed != null);
|
||||||
} else if (valIsUndef) {
|
} else if (valIsUndef) {
|
||||||
setLow = isReflexive && (retHighest || computed !== undefined);
|
setLow = isReflexive && (retHighest || isDef);
|
||||||
|
} else if (computed == null) {
|
||||||
|
setLow = false;
|
||||||
} else {
|
} else {
|
||||||
setLow = retHighest ? (computed <= value) : (computed < value);
|
setLow = retHighest ? (computed <= value) : (computed < value);
|
||||||
}
|
}
|
||||||
|
|||||||
19
test/test.js
19
test/test.js
@@ -14415,9 +14415,12 @@
|
|||||||
deepEqual(actual, [3, 1, 2]);
|
deepEqual(actual, [3, 1, 2]);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('should move `undefined` and `NaN` values to the end', 1, function() {
|
test('should move `null`, `undefined`, and `NaN` values to the end', 2, function() {
|
||||||
var array = [NaN, undefined, 4, 1, undefined, 3, NaN, 2];
|
var array = [NaN, undefined, null, 4, null, 1, undefined, 3, NaN, 2];
|
||||||
deepEqual(_.sortBy(array), [1, 2, 3, 4, undefined, undefined, NaN, NaN]);
|
deepEqual(_.sortBy(array), [1, 2, 3, 4, null, null, undefined, undefined, NaN, NaN]);
|
||||||
|
|
||||||
|
array = [NaN, undefined, null, 'd', null, 'a', undefined, 'c', NaN, 'b'];
|
||||||
|
deepEqual(_.sortBy(array), ['a', 'b', 'c', 'd', null, null, undefined, undefined, NaN, NaN]);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('should treat number values for `collection` as empty', 1, function() {
|
test('should treat number values for `collection` as empty', 1, function() {
|
||||||
@@ -14615,16 +14618,16 @@
|
|||||||
});
|
});
|
||||||
|
|
||||||
test('`_.' + methodName + '` should align with `_.sortBy`', 8, function() {
|
test('`_.' + methodName + '` should align with `_.sortBy`', 8, function() {
|
||||||
var expected = [1, '2', {}, undefined, NaN, NaN];
|
var expected = [1, '2', {}, null, undefined, NaN, NaN];
|
||||||
|
|
||||||
_.each([
|
_.each([
|
||||||
[NaN, 1, '2', {}, NaN, undefined],
|
[NaN, null, 1, '2', {}, NaN, undefined],
|
||||||
['2', 1, NaN, {}, NaN, undefined]
|
['2', null, 1, NaN, {}, NaN, undefined]
|
||||||
], function(array) {
|
], function(array) {
|
||||||
deepEqual(_.sortBy(array), expected);
|
deepEqual(_.sortBy(array), expected);
|
||||||
strictEqual(func(expected, 3), 2);
|
strictEqual(func(expected, 3), 2);
|
||||||
strictEqual(func(expected, undefined), isSortedIndex ? 3 : 4);
|
strictEqual(func(expected, undefined), isSortedIndex ? 4 : 5);
|
||||||
strictEqual(func(expected, NaN), isSortedIndex ? 4 : 6);
|
strictEqual(func(expected, NaN), isSortedIndex ? 5 : 7);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user