The lodash method orderBy exported as a module for sorting collections by multiple iteratees with configurable sort orders
npx @tessl/cli install tessl/npm-lodash--orderby@4.6.0The lodash method orderBy exported as a standalone module for sorting collections by multiple iteratees with configurable sort orders. This utility extends the functionality of sortBy by allowing specification of sort direction (ascending or descending) for each criterion.
npm install lodash.orderbyconst orderBy = require('lodash.orderby');For ES6 environments:
import orderBy from 'lodash.orderby';const orderBy = require('lodash.orderby');
const users = [
{ 'user': 'fred', 'age': 48 },
{ 'user': 'barney', 'age': 34 },
{ 'user': 'fred', 'age': 42 },
{ 'user': 'barney', 'age': 36 }
];
// Sort by user ascending, then by age descending
const result = orderBy(users, ['user', 'age'], ['asc', 'desc']);
// => [
// { 'user': 'barney', 'age': 36 },
// { 'user': 'barney', 'age': 34 },
// { 'user': 'fred', 'age': 48 },
// { 'user': 'fred', 'age': 42 }
// ]
// Sort by single property with specified order
const byUser = orderBy(users, 'user', 'desc');
// => sorted by user in descending order
// Default ascending order when orders not specified
const defaultSort = orderBy(users, ['user', 'age']);
// => sorted by user ascending, then age ascendingThe orderBy function sorts collections by multiple iteratees with configurable sort orders.
/**
* Creates an array of elements, sorted in ascending or descending order by the results
* of running each element in a collection thru each iteratee. This method performs a
* stable sort, that is, it preserves the original sort order of equal elements. The
* iteratees are invoked with one argument: (value).
*
* @param {Array|Object} collection The collection to iterate over.
* @param {Array[]|Function[]|Object[]|string[]} [iteratees=[_.identity]]
* The iteratees to sort by.
* @param {string[]} [orders] The sort orders of `iteratees`.
* @returns {Array} Returns the new sorted array.
*/
function orderBy(collection, iteratees, orders);Parameters:
collection (Array|Object): The collection to iterate over. Can be an array of objects, array of primitives, or object with enumerable properties.[iteratees=[_.identity]] (Array[]|Function[]|Object[]|string[]): The iteratees to sort by. Can be:
'user', 'user.name', 'address.city')[orders] (string[]): The sort orders of iteratees. Values can be:
'asc': Ascending order (default if not specified)'desc': Descending orderReturns: (Array) Returns a new sorted array. Original collection is not modified.
Usage Examples:
const orderBy = require('lodash.orderby');
// Example data
const products = [
{ name: 'iPhone', price: 999, category: 'Electronics', rating: 4.5 },
{ name: 'MacBook', price: 1299, category: 'Electronics', rating: 4.8 },
{ name: 'Desk', price: 299, category: 'Furniture', rating: 4.2 },
{ name: 'Chair', price: 199, category: 'Furniture', rating: 4.0 }
];
// Sort by category ascending, then price descending
const sorted1 = orderBy(products, ['category', 'price'], ['asc', 'desc']);
// Sort using custom function
const sorted2 = orderBy(products, [
'category',
(product) => product.rating * product.price
], ['asc', 'desc']);
// Sort by nested property path
const users = [
{ profile: { name: 'Alice', stats: { score: 95 } } },
{ profile: { name: 'Bob', stats: { score: 87 } } }
];
const sortedUsers = orderBy(users, 'profile.stats.score', 'desc');
// Sort array of primitives
const numbers = [3, 1, 4, 1, 5, 9, 2, 6];
const sortedNumbers = orderBy(numbers, null, 'desc');
// => [9, 6, 5, 4, 3, 2, 1, 1]
// Mixed iteratees and orders
const mixed = orderBy(products,
['category', (p) => p.name.length, 'price'],
['asc', 'desc', 'asc']
);Error Handling:
[] when collection is null or undefinediteratees parameter as identity function (sorts by element value)orders array is shorter than iteratees arrayPerformance Characteristics:
Type Coercion and Edge Cases:
// Handles null/undefined values gracefully
const withNulls = [
{ name: 'Alice', score: 95 },
{ name: null, score: 87 },
{ name: 'Bob', score: null }
];
const sorted = orderBy(withNulls, ['name', 'score'], ['asc', 'desc']);
// String objects as orders (less common usage)
const stringOrder = orderBy(users, 'name', Object('desc'));
// Works with object collections (iterates over enumerable properties)
const objCollection = {
a: { value: 3 },
b: { value: 1 },
c: { value: 2 }
};
const sortedObj = orderBy(objCollection, 'value', 'asc');
// => [{ value: 1 }, { value: 2 }, { value: 3 }]