react / wstein / node_modules / jest-cli / node_modules / istanbul / node_modules / js-yaml / node_modules / argparse / node_modules / lodash / collection / sortBy.js
80742 viewsvar baseCallback = require('../internal/baseCallback'),1baseMap = require('../internal/baseMap'),2baseSortBy = require('../internal/baseSortBy'),3compareAscending = require('../internal/compareAscending'),4isIterateeCall = require('../internal/isIterateeCall');56/**7* Creates an array of elements, sorted in ascending order by the results of8* running each element in a collection through `iteratee`. This method performs9* a stable sort, that is, it preserves the original sort order of equal elements.10* The `iteratee` is bound to `thisArg` and invoked with three arguments:11* (value, index|key, collection).12*13* If a property name is provided for `iteratee` the created `_.property`14* style callback returns the property value of the given element.15*16* If a value is also provided for `thisArg` the created `_.matchesProperty`17* style callback returns `true` for elements that have a matching property18* value, else `false`.19*20* If an object is provided for `iteratee` the created `_.matches` style21* callback returns `true` for elements that have the properties of the given22* object, else `false`.23*24* @static25* @memberOf _26* @category Collection27* @param {Array|Object|string} collection The collection to iterate over.28* @param {Function|Object|string} [iteratee=_.identity] The function invoked29* per iteration.30* @param {*} [thisArg] The `this` binding of `iteratee`.31* @returns {Array} Returns the new sorted array.32* @example33*34* _.sortBy([1, 2, 3], function(n) {35* return Math.sin(n);36* });37* // => [3, 1, 2]38*39* _.sortBy([1, 2, 3], function(n) {40* return this.sin(n);41* }, Math);42* // => [3, 1, 2]43*44* var users = [45* { 'user': 'fred' },46* { 'user': 'pebbles' },47* { 'user': 'barney' }48* ];49*50* // using the `_.property` callback shorthand51* _.pluck(_.sortBy(users, 'user'), 'user');52* // => ['barney', 'fred', 'pebbles']53*/54function sortBy(collection, iteratee, thisArg) {55if (collection == null) {56return [];57}58if (thisArg && isIterateeCall(collection, iteratee, thisArg)) {59iteratee = null;60}61var index = -1;62iteratee = baseCallback(iteratee, thisArg, 3);6364var result = baseMap(collection, function(value, key, collection) {65return { 'criteria': iteratee(value, key, collection), 'index': ++index, 'value': value };66});67return baseSortBy(result, compareAscending);68}6970module.exports = sortBy;717273