JavaScript Natural Sort

0.7.1 · abandoned · verified Sun Apr 19

The `javascript-natural-sort` package provides a comparator function implementing a natural sort algorithm, designed for use with JavaScript's `Array.prototype.sort()`. It intelligently handles various complex string patterns beyond simple lexicographical sorting, including numerical values (integers, floats, scientific notation), IP addresses, filenames, dates, and currency. The current stable version is `0.7.1`. Given its last update over six years ago, the package is considered abandoned and does not receive active maintenance or new feature development. Its key differentiator is its comprehensive approach to handling diverse data types within a single, self-contained sorting utility, offering a simple API for common sorting challenges where standard string comparison falls short, with an option for case-insensitive sorting.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates basic natural sorting for mixed numeric data and IP addresses, plus how to enable and disable global case-insensitive sorting.

import naturalSort from 'javascript-natural-sort';

// Basic numeric string and number sorting
const mixedNumerics = ['10', 9, '2', 1, '4'];
const sortedNumerics = mixedNumerics.sort(naturalSort);
console.log('Sorted Numerics:', sortedNumerics); // Expected: [1, '2', '4', 9, '10']

// Sorting IP addresses
const ipAddresses = ['192.168.0.100', '192.168.0.1', '192.168.1.1'];
const sortedIPs = ipAddresses.sort(naturalSort);
console.log('Sorted IPs:', sortedIPs); // Expected: ['192.168.0.1', '192.168.0.100', '192.168.1.1']

// Case-insensitive sorting (modifies global state)
naturalSort.insensitive = true;
const mixedCase = ['a', 'B', 'C', 'd'];
const sortedMixedCase = mixedCase.sort(naturalSort);
console.log('Sorted Mixed Case (insensitive):', sortedMixedCase); // Expected: ['a', 'B', 'C', 'd']

// Remember to reset if needed to avoid affecting other sorts
naturalSort.insensitive = false;

view raw JSON →