Registry / data / javascript-natural-sort

javascript-natural-sort

JSON →
library0.7.1jsnpmunverified

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.

npm install javascript-natural-sort
INSTALL
IMPORT
SIG · JAVASCRIPT-NATURAL
J
javascript-natural-sort
datajavascriptv0.7.1
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

naturalSort
import naturalSort from 'javascript-natural-sort';
import { naturalSort } from 'javascript-natural-sort';
The package exports a default function. While technically a CJS default export, modern bundlers and Node.js often allow this ESM syntax.
naturalSort
const naturalSort = require('javascript-natural-sort');
const { naturalSort } = require('javascript-natural-sort');
The package uses CommonJS `module.exports = naturalSort;` for its main entry point.
naturalSort.insensitive
naturalSort.insensitive = true;
naturalSort({ insensitive: true });
Case-insensitivity is enabled by setting a global property directly on the `naturalSort` function, not via a function parameter.

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;
Debug
Known issues
gotchaThe `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.
fix
Always explicitly set `naturalSort.insensitive = false;` after performing case-insensitive sorts if subsequent sorts require case-sensitive behavior, or wrap your usage in a function that manages the state.
affects: >=0.1.0
breakingThis 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.
fix
Consider migrating to a more actively maintained natural sort library that supports modern JavaScript features and best practices, or be prepared to fork and maintain the code yourself.
affects: >=0.7.1
gotchaThe 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.
fix
Use CommonJS `require` for Node.js environments, or rely on a bundler (like Webpack, Rollup, or esbuild) to correctly handle the CJS-to-ESM transformation for browser environments.
affects: >=0.1.0
Errors
Common errors & fixes
TypeError: naturalSort is not a function
Incorrectly importing the `naturalSort` function, typically by trying to use named imports (`{ naturalSort }`) when it is a default export.
fix
For 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'
The `naturalSort` function is case-sensitive by default, leading to uppercase letters sorting before lowercase letters.
fix
Enable case-insensitive sorting by setting the global flag: `naturalSort.insensitive = true;` (remember to reset it if needed).
TypeError: naturalSort.insensitive is not callable
Attempting to call `naturalSort.insensitive()` as a function instead of assigning a boolean value to it.
fix
Set the property directly: `naturalSort.insensitive = true;` (or `false`).
Upgrade
Version history
0.7.1latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
4 hits · last 30 days
node
4
Resources
javascript-natural-sort — npm install javascript-natural-sort · libregistry