The modern build of lodash's sortBy function for creating sorted arrays by iteratee results.
npx @tessl/cli install tessl/npm-lodash-sortby@3.1.0The modern build of lodash's sortBy function exported as a standalone module. This package provides a stable sorting utility that creates arrays of elements sorted in ascending order by the results of running each element through an iteratee function. It supports various callback styles including property shortcuts, matches callbacks, and matchesProperty callbacks.
npm install lodash.sortbyvar sortBy = require('lodash.sortby');In modern environments with ES module interop:
import sortBy from 'lodash.sortby';var sortBy = require('lodash.sortby');
// Sort by function iteratee
var users = [
{ 'user': 'fred', 'age': 48 },
{ 'user': 'barney', 'age': 36 },
{ 'user': 'fred', 'age': 40 },
{ 'user': 'barney', 'age': 34 }
];
// Sort by age
var result = sortBy(users, function(o) { return o.age; });
// => [{ 'user': 'barney', 'age': 34 }, { 'user': 'barney', 'age': 36 }, { 'user': 'fred', 'age': 40 }, { 'user': 'fred', 'age': 48 }]
// Sort by property shorthand
var result = sortBy(users, 'age');
// => [{ 'user': 'barney', 'age': 34 }, { 'user': 'barney', 'age': 36 }, { 'user': 'fred', 'age': 40 }, { 'user': 'fred', 'age': 48 }]Creates an array of elements sorted in ascending order by the results of running each element through an iteratee function. Performs stable sorting that preserves the original order of equal elements.
/**
* Creates an array of elements, sorted in ascending order by the results of
* running each element in a collection through `iteratee`. This method performs
* a stable sort, that is, it preserves the original sort order of equal elements.
* The `iteratee` is bound to `thisArg` and invoked with three arguments:
* (value, index|key, collection).
*
* @param {Array|Object|string} collection The collection to iterate over.
* @param {Function|Object|string} [iteratee=_.identity] The function invoked per iteration.
* @param {*} [thisArg] The `this` binding of `iteratee`.
* @returns {Array} Returns the new sorted array.
*/
function sortBy(collection, iteratee, thisArg);Parameters:
collection (Array|Object|string): The collection to iterate over. If null or undefined, returns empty array.iteratee (Function|Object|string) [optional]: The function invoked per iteration. Defaults to identity function. Supports multiple callback styles:
_.matches style callbackthisArg (*) [optional]: The this binding context for the iteratee functionReturns:
Array: A new sorted array containing all elements from the collection sorted in ascending order by the iteratee resultsIteratee Callback Styles:
Function Callback:
sortBy([1, 2, 3], function(n) { return Math.sin(n); });Property Shorthand:
sortBy(users, 'user'); // sorts by 'user' propertyMatches Object:
sortBy(users, { 'active': true }); // sorts by matching { 'active': true }MatchesProperty:
sortBy(users, 'active', true); // sorts by 'active' property matching trueKey Features:
Usage Examples:
// Sort numbers by mathematical function
sortBy([1, 2, 3], function(n) {
return Math.sin(n);
});
// => [3, 1, 2]
// Sort with thisArg binding
sortBy([1, 2, 3], function(n) {
return this.sin(n);
}, Math);
// => [3, 1, 2]
// Sort objects by property
var users = [
{ 'user': 'fred' },
{ 'user': 'pebbles' },
{ 'user': 'barney' }
];
// Using property shorthand
sortBy(users, 'user');
// => [{ 'user': 'barney' }, { 'user': 'fred' }, { 'user': 'pebbles' }]
// Demonstrating stable sort behavior with equal elements
var items = [
{ name: 'apple', score: 5, id: 1 },
{ name: 'banana', score: 3, id: 2 },
{ name: 'cherry', score: 5, id: 3 },
{ name: 'date', score: 3, id: 4 }
];
// Equal scores maintain original order (stable sort)
sortBy(items, 'score');
// => [{ name: 'banana', score: 3, id: 2 }, { name: 'date', score: 3, id: 4 },
// { name: 'apple', score: 5, id: 1 }, { name: 'cherry', score: 5, id: 3 }]
// Using property shorthand with lodash pluck pattern
var users = [
{ 'user': 'fred' },
{ 'user': 'pebbles' },
{ 'user': 'barney' }
];
// Extract sorted property values (requires additional lodash.pluck or map)
var sortedUsers = sortBy(users, 'user');
// => [{ 'user': 'barney' }, { 'user': 'fred' }, { 'user': 'pebbles' }]
// Empty/null collections
sortBy(null); // => []
sortBy(undefined); // => []
sortBy([]); // => []