lodash vs underscore vs natural-orderby vs sort-by
Sorting and Ordering Utilities Comparison
1 Year
lodashunderscorenatural-orderbysort-bySimilar Packages:
What's Sorting and Ordering Utilities?

Sorting and ordering utilities in JavaScript help developers arrange data in a specific sequence, whether it’s ascending, descending, or based on custom criteria. These utilities are essential for tasks like organizing lists, tables, or any collection of items. They provide functions to sort arrays, objects, or other data structures based on one or more keys, values, or complex algorithms. Libraries like lodash, underscore, and natural-orderby offer robust sorting capabilities, each with unique features and performance characteristics. These tools enhance data manipulation, improve user interfaces, and streamline tasks that require ordered data.

Package Weekly Downloads Trend
Github Stars Ranking
Stat Detail
Package
Downloads
Stars
Size
Issues
Publish
License
lodash67,292,15760,318-964 years agoMIT
underscore14,151,72327,383906 kB529 months agoMIT
natural-orderby2,033,4676272.5 kB165 months agoMIT
sort-by65,589225-12-MIT
Feature Comparison: lodash vs underscore vs natural-orderby vs sort-by

Size and Performance

  • lodash:

    lodash is a larger library compared to others, but it is highly optimized for performance. It provides a wide range of functions, including sorting, which are implemented with efficiency in mind. The trade-off in size is often worth it for the extensive functionality and performance gains in complex operations.

  • underscore:

    underscore is similar in size to lodash, but it offers fewer features overall. It is well-optimized for the functions it provides, including sorting. While it may not be as fast as lodash for all operations, it is still efficient and suitable for most use cases, especially in smaller projects.

  • natural-orderby:

    natural-orderby is a small and focused library specifically designed for natural sorting. Its lightweight nature makes it ideal for projects where only natural ordering is needed, without the overhead of a larger utility library. This makes it highly efficient for sorting tasks that require minimal resources.

  • sort-by:

    sort-by is a minimalistic library that provides a simple API for sorting. Its small size and straightforward implementation make it fast and efficient for sorting tasks, especially when you need to sort by one or two keys without any complex logic.

Sorting Algorithm

  • lodash:

    lodash uses a stable sorting algorithm for its sorting functions, which ensures that elements with the same value retain their original order. This is particularly important for multi-key sorting, where maintaining the relative order of equal elements is desired. The stability of the algorithm enhances the reliability of sorting operations, making lodash a robust choice for complex data structures.

  • underscore:

    underscore also uses a stable sorting algorithm for its sorting functions, similar to lodash. This ensures that the order of equal elements is preserved, making it reliable for both single-key and multi-key sorting. The stability of the algorithm, combined with underscore's functional programming approach, makes it a solid choice for projects that require predictable and consistent sorting behavior.

  • natural-orderby:

    natural-orderby implements a natural sorting algorithm that sorts strings in a way that is intuitive to humans, especially when dealing with mixed content (e.g., file2, file10, file1). This type of sorting is particularly useful in applications where the order of items needs to reflect natural language conventions, making it ideal for user interfaces that display lists of files, names, or other mixed-content data.

  • sort-by:

    sort-by uses a simple comparison-based sorting algorithm, which is efficient for sorting arrays by one or more keys. However, it does not guarantee stability, meaning that the relative order of equal elements may not be preserved. This makes it suitable for straightforward sorting tasks, but it may not be ideal for scenarios where stable sorting is required.

Ease of Use: Code Examples

  • lodash:

    Sorting with lodash

    const _ = require('lodash');
    
    const items = [
      { name: 'apple', value: 10 },
      { name: 'banana', value: 5 },
      { name: 'cherry', value: 15 },
    ];
    
    // Sort by value
    const sortedByValue = _.sortBy(items, 'value');
    console.log(sortedByValue);
    
    // Sort by name
    const sortedByName = _.sortBy(items, 'name');
    console.log(sortedByName);
    
  • underscore:

    Sorting with underscore

    const _ = require('underscore');
    
    const items = [
      { name: 'apple', value: 10 },
      { name: 'banana', value: 5 },
      { name: 'cherry', value: 15 },
    ];
    
    // Sort by value
    const sortedByValue = _.sortBy(items, 'value');
    console.log(sortedByValue);
    
    // Sort by name
    const sortedByName = _.sortBy(items, 'name');
    console.log(sortedByName);
    
  • natural-orderby:

    Natural Sorting with natural-orderby

    const naturalOrderBy = require('natural-orderby');
    
    const items = [
      'file10',
      'file2',
      'file1',
      'file20',
      'file11',
    ];
    
    // Sort using natural order
    const sortedNaturally = items.sort(naturalOrderBy);
    console.log(sortedNaturally);
    
  • sort-by:

    Sorting with sort-by

    const sortBy = require('sort-by');
    
    const items = [
      { name: 'apple', value: 10 },
      { name: 'banana', value: 5 },
      { name: 'cherry', value: 15 },
    ];
    
    // Sort by value
    const sortedByValue = items.sort(sortBy('value'));
    console.log(sortedByValue);
    
    // Sort by name
    const sortedByName = items.sort(sortBy('name'));
    console.log(sortedByName);
    
How to Choose: lodash vs underscore vs natural-orderby vs sort-by
  • lodash:

    Choose lodash if you need a comprehensive utility library that offers a wide range of functions, including advanced sorting capabilities. It’s ideal for projects that require not only sorting but also other utility functions, all while maintaining performance and modularity.

  • underscore:

    Opt for underscore if you prefer a library that provides a solid set of utility functions, including sorting, with a functional programming approach. It’s a good choice for projects that value simplicity and readability, though it may not be as feature-rich as lodash.

  • natural-orderby:

    Select natural-orderby if you need to sort data in a way that respects natural ordering, especially for strings with mixed content (e.g., numbers and letters). It’s perfect for applications where human-friendly sorting is essential, such as file explorers or user interfaces that display lists.

  • sort-by:

    Use sort-by if you want a lightweight and straightforward solution for sorting arrays by one or more keys. It’s easy to use and integrates well into projects that need simple sorting without the overhead of a larger library.

README for lodash

lodash v4.17.21

The Lodash library exported as Node.js modules.

Installation

Using npm:

$ npm i -g npm
$ npm i --save lodash

In Node.js:

// Load the full build.
var _ = require('lodash');
// Load the core build.
var _ = require('lodash/core');
// Load the FP build for immutable auto-curried iteratee-first data-last methods.
var fp = require('lodash/fp');

// Load method categories.
var array = require('lodash/array');
var object = require('lodash/fp/object');

// Cherry-pick methods for smaller browserify/rollup/webpack bundles.
var at = require('lodash/at');
var curryN = require('lodash/fp/curryN');

See the package source for more details.

Note:
Install n_ for Lodash use in the Node.js < 6 REPL.

Support

Tested in Chrome 74-75, Firefox 66-67, IE 11, Edge 18, Safari 11-12, & Node.js 8-12.
Automated browser & CI test runs are available.