JavaScript Natural Sort
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
-
TypeError: naturalSort is not a function
cause Incorrectly importing the `naturalSort` function, typically by trying to use named imports (`{ naturalSort }`) when it is a default export.fixFor CommonJS, use `const naturalSort = require('javascript-natural-sort');`. For ESM, use `import naturalSort from 'javascript-natural-sort';`. -
Array.prototype.sort() results in 'A', 'b', 'C' instead of 'A', 'C', 'b'
cause The `naturalSort` function is case-sensitive by default, leading to uppercase letters sorting before lowercase letters.fixEnable case-insensitive sorting by setting the global flag: `naturalSort.insensitive = true;` (remember to reset it if needed). -
TypeError: naturalSort.insensitive is not callable
cause Attempting to call `naturalSort.insensitive()` as a function instead of assigning a boolean value to it.fixSet the property directly: `naturalSort.insensitive = true;` (or `false`).
Warnings
- gotcha The `naturalSort.insensitive` property sets a global flag for case-insensitive sorting. This means if you set it to `true`, all subsequent calls to `naturalSort` will be case-insensitive until you explicitly set it back to `false`. This global state can lead to unexpected behavior in applications with multiple concurrent sorts or long-running processes if not managed carefully.
- breaking This package is considered abandoned, with the last commit over six years ago. It means it will not receive updates for new JavaScript features, bug fixes, or security patches. Using it in modern applications might lead to compatibility issues with newer Node.js versions or browser environments, and it will not address new edge cases in natural sorting.
- gotcha The package does not have an explicit `module` field in its `package.json`, indicating a lack of native ESM support. While bundlers often handle CommonJS interoperability, direct ESM imports in Node.js environments without a bundler might behave differently or require specific configurations on older Node.js versions.
Install
-
npm install javascript-natural-sort -
yarn add javascript-natural-sort -
pnpm add javascript-natural-sort
Imports
- naturalSort
import { naturalSort } from 'javascript-natural-sort';import naturalSort from 'javascript-natural-sort';
- naturalSort
const { naturalSort } = require('javascript-natural-sort');const naturalSort = require('javascript-natural-sort'); - naturalSort.insensitive
naturalSort({ insensitive: true });naturalSort.insensitive = true;
Quickstart
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;