From 1580453f4b8066d4939b0e5c0889fc05cc372173 Mon Sep 17 00:00:00 2001 From: John-David Dalton Date: Sun, 27 Jul 2014 19:40:10 -0700 Subject: [PATCH] Ensure `_.sortedIndex` and `_.sortedLastIndex` align with `_.sortBy`. --- lodash.js | 10 ++++++---- test/test.js | 20 +++++++++++++++++++- 2 files changed, 25 insertions(+), 5 deletions(-) diff --git a/lodash.js b/lodash.js index a3832681a..84c14da22 100644 --- a/lodash.js +++ b/lodash.js @@ -428,7 +428,7 @@ * @returns {number} Returns the sort order indicator for `object`. */ function compareAscending(object, other) { - return baseCompareAscending(object.criteria, other.criteria) || object.index - other.index; + return baseCompareAscending(object.criteria, other.criteria) || (object.index - other.index); } /** @@ -2404,12 +2404,14 @@ while (low < high) { var mid = (low + high) >>> 1, - computed = iterator(array[mid]); + computed = iterator(array[mid]), + setLow = retHighest ? computed <= value : computed < value; if (hintNum && typeof computed != 'undefined') { - computed = +computed || 0; + computed = +computed; + setLow = computed != computed || setLow; } - if (retHighest ? computed <= value : computed < value) { + if (setLow) { low = mid + 1; } else { high = mid; diff --git a/test/test.js b/test/test.js index fb5005a49..b5ceecd8e 100644 --- a/test/test.js +++ b/test/test.js @@ -9684,7 +9684,25 @@ strictEqual(actual, 1); }); - test('`_.' + methodName + '` supports arrays with lengths larger than `Math.pow(2, 31) - 1`', 1, function() { + test('`_.' + methodName + '` should align with `_.sortBy`', 8, function() { + var array = [NaN, {}, NaN, 1, 2, undefined]; + deepEqual(_.sortBy(array), array); + strictEqual(func(array, 3), 5); + + array = [1, 2, NaN, {}, NaN, undefined]; + deepEqual(_.sortBy(array), array); + strictEqual(func(array, 3), 5); + + array = [NaN, {}, NaN, '1', '2', undefined]; + deepEqual(_.sortBy(array), array); + strictEqual(func(array, '3'), 5); + + array = ['1', '2', NaN, {}, NaN, undefined]; + deepEqual(_.sortBy(array), array); + strictEqual(func(array, '3'), 2); + }); + + test('`_.' + methodName + '` should support arrays larger than `Math.pow(2, 31) - 1`', 1, function() { var length = Math.pow(2, 32) - 1, index = length - 1, array = Array(length),