or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

index.md
tile.json

tessl/npm-lodash--orderby

The lodash method orderBy exported as a module for sorting collections by multiple iteratees with configurable sort orders

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/lodash.orderby@4.6.x

To install, run

npx @tessl/cli install tessl/npm-lodash--orderby@4.6.0

index.mddocs/

Lodash OrderBy

The 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.

Package Information

  • Package Name: lodash.orderby
  • Package Type: npm
  • Language: JavaScript
  • Installation: npm install lodash.orderby

Core Imports

const orderBy = require('lodash.orderby');

For ES6 environments:

import orderBy from 'lodash.orderby';

Basic Usage

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 ascending

Capabilities

Multi-Criteria Sorting

The 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:
    • String: Property path (e.g., 'user', 'user.name', 'address.city')
    • Function: Custom sorting function that returns a comparable value
    • Array: Multiple criteria combining strings and functions
    • Object: Property descriptor (advanced usage)
  • [orders] (string[]): The sort orders of iteratees. Values can be:
    • 'asc': Ascending order (default if not specified)
    • 'desc': Descending order
    • Array of orders corresponding to each iteratee

Returns: (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:

  • Returns empty array [] when collection is null or undefined
  • Treats missing iteratees parameter as identity function (sorts by element value)
  • Defaults to ascending order when orders array is shorter than iteratees array
  • Handles mixed data types consistently with JavaScript's native comparison rules

Performance Characteristics:

  • Uses stable sorting algorithm that preserves relative order of equal elements
  • Time complexity: O(n log n) where n is the collection length
  • Space complexity: O(n) for creating the sorted result array
  • Optimized for multiple sort criteria with efficient comparison functions

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 }]