From 94bc65d851375a14e68e61c7e98f09d549062e08 Mon Sep 17 00:00:00 2001 From: rektide Date: Sat, 7 Feb 2015 14:04:15 -0500 Subject: [PATCH] Feature: sortOrder. --- modern/collection.js | 1 + modern/collection/sortOrder.js | 29 +++++++++++++++++++++++++++++ 2 files changed, 30 insertions(+) create mode 100644 modern/collection/sortOrder.js diff --git a/modern/collection.js b/modern/collection.js index 2682db50..38b19f61 100644 --- a/modern/collection.js +++ b/modern/collection.js @@ -38,5 +38,6 @@ module.exports = { 'some': require('./collection/some'), 'sortBy': require('./collection/sortBy'), 'sortByAll': require('./collection/sortByAll'), + 'sortOrder': require('./collection/sortOrder'), 'where': require('./collection/where') }; diff --git a/modern/collection/sortOrder.js b/modern/collection/sortOrder.js new file mode 100644 index 00000000..9f5f68a8 --- /dev/null +++ b/modern/collection/sortOrder.js @@ -0,0 +1,29 @@ +var baseEach = require('../internal/baseEach'), + baseBinaryIndexBy = require('../internal/binaryIndexBy'); + +/** + * Create a sorted index of array values, ascending from the index of the + * lowest item in the array up through index of the highest item in the array. + * @static + * @memberOf + * @category Collection + * @param {Array|Object|string} collection The collection to iterate over. + * @param {Function} [iteratee=_.identity] The function + * invoked per iteration. + * @returns {Array} Returns the sorted index array. + * @example + * + * _.sortOrder([6, 4, 5]); + * // => [1, 2, 0] + */ +function sortOrder(collection, iteratee) { + var indexes = [] + baseEach(collection, function(value, key) { + var index = baseBinaryIndexBy(indexes, key, function(value) { value = collection[value]; return iteratee ? iteratee(value) : value; }); + indexes.splice(index, 0, key); + }); + + return indexes; +} + +module.exports = sortOrder;