{"id":11132,"library":"javascript-natural-sort","title":"JavaScript Natural Sort","description":"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.","status":"abandoned","version":"0.7.1","language":"javascript","source_language":"en","source_url":"https://github.com/Bill4Time/javascript-natural-sort","tags":["javascript","natural","sort","array","sorting"],"install":[{"cmd":"npm install javascript-natural-sort","lang":"bash","label":"npm"},{"cmd":"yarn add javascript-natural-sort","lang":"bash","label":"yarn"},{"cmd":"pnpm add javascript-natural-sort","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"The package exports a default function. While technically a CJS default export, modern bundlers and Node.js often allow this ESM syntax.","wrong":"import { naturalSort } from 'javascript-natural-sort';","symbol":"naturalSort","correct":"import naturalSort from 'javascript-natural-sort';"},{"note":"The package uses CommonJS `module.exports = naturalSort;` for its main entry point.","wrong":"const { naturalSort } = require('javascript-natural-sort');","symbol":"naturalSort","correct":"const naturalSort = require('javascript-natural-sort');"},{"note":"Case-insensitivity is enabled by setting a global property directly on the `naturalSort` function, not via a function parameter.","wrong":"naturalSort({ insensitive: true });","symbol":"naturalSort.insensitive","correct":"naturalSort.insensitive = true;"}],"quickstart":{"code":"import naturalSort from 'javascript-natural-sort';\n\n// Basic numeric string and number sorting\nconst mixedNumerics = ['10', 9, '2', 1, '4'];\nconst sortedNumerics = mixedNumerics.sort(naturalSort);\nconsole.log('Sorted Numerics:', sortedNumerics); // Expected: [1, '2', '4', 9, '10']\n\n// Sorting IP addresses\nconst ipAddresses = ['192.168.0.100', '192.168.0.1', '192.168.1.1'];\nconst sortedIPs = ipAddresses.sort(naturalSort);\nconsole.log('Sorted IPs:', sortedIPs); // Expected: ['192.168.0.1', '192.168.0.100', '192.168.1.1']\n\n// Case-insensitive sorting (modifies global state)\nnaturalSort.insensitive = true;\nconst mixedCase = ['a', 'B', 'C', 'd'];\nconst sortedMixedCase = mixedCase.sort(naturalSort);\nconsole.log('Sorted Mixed Case (insensitive):', sortedMixedCase); // Expected: ['a', 'B', 'C', 'd']\n\n// Remember to reset if needed to avoid affecting other sorts\nnaturalSort.insensitive = false;","lang":"javascript","description":"Demonstrates basic natural sorting for mixed numeric data and IP addresses, plus how to enable and disable global case-insensitive sorting."},"warnings":[{"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.","message":"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.","severity":"gotcha","affected_versions":">=0.1.0"},{"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.","message":"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.","severity":"breaking","affected_versions":">=0.7.1"},{"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.","message":"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.","severity":"gotcha","affected_versions":">=0.1.0"}],"env_vars":null,"last_verified":"2026-04-19T00:00:00.000Z","next_check":"2026-07-18T00:00:00.000Z","problems":[{"fix":"For CommonJS, use `const naturalSort = require('javascript-natural-sort');`. For ESM, use `import naturalSort from 'javascript-natural-sort';`.","cause":"Incorrectly importing the `naturalSort` function, typically by trying to use named imports (`{ naturalSort }`) when it is a default export.","error":"TypeError: naturalSort is not a function"},{"fix":"Enable case-insensitive sorting by setting the global flag: `naturalSort.insensitive = true;` (remember to reset it if needed).","cause":"The `naturalSort` function is case-sensitive by default, leading to uppercase letters sorting before lowercase letters.","error":"Array.prototype.sort() results in 'A', 'b', 'C' instead of 'A', 'C', 'b'"},{"fix":"Set the property directly: `naturalSort.insensitive = true;` (or `false`).","cause":"Attempting to call `naturalSort.insensitive()` as a function instead of assigning a boolean value to it.","error":"TypeError: naturalSort.insensitive is not callable"}],"ecosystem":"npm"}