# isotropic-natural-sort

> A configurable natural sorting implementation

Latest version **0.9.1** (published 2026-08-24) · BSD-3-Clause license · 0 weekly downloads

## Install

```sh
npm install isotropic-natural-sort
pnpm add isotropic-natural-sort
yarn add isotropic-natural-sort
bun add isotropic-natural-sort
```

## Health

**Score 50/100 (C)** — status: active.

Positive: esm support; no vulnerabilities.

Warnings: low downloads; no types; pre 1.0.

## Facts

| | |
|---|---|
| Version | 0.9.1 |
| Published | 2026-08-24 |
| First published | 2017-09-12 |
| Weekly downloads | 0 |
| License | BSD-3-Clause |
| TypeScript types | none |
| Module format | ESM + CommonJS |
| Node | ^26.7.0 |
| Dependencies | 1 |
| Unpacked size | 18.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1 |
| Author | Steven Olmsted |
| Maintainers | ibigroup |
| Keywords | comparator, human-sort, isotropic, lexicographic-ordering, natural-sort, sort |

## Links

- npm: https://www.npmjs.com/package/isotropic-natural-sort
- Repository: https://github.com/ibi-group/isotropic-natural-sort
- Issues: https://github.com/ibi-group/isotropic-natural-sort/issues
- npm.io page: https://npm.io/package/isotropic-natural-sort

## Dependencies (1)

- [isotropic-character-fold](https://npm.io/package/isotropic-character-fold.md) ~0.9.1

## Alternatives

- [d3-force-3d](https://npm.io/package/d3-force-3d.md) — 1.0M weekly downloads
- [ng2-charts](https://npm.io/package/ng2-charts.md) — 486.8K weekly downloads
- [@arcgis/core](https://npm.io/package/@arcgis/core.md) — 257.8K weekly downloads
- [react-sparklines](https://npm.io/package/react-sparklines.md) — 249.3K weekly downloads
- [react-native-gifted-charts](https://npm.io/package/react-native-gifted-charts.md) — 182.3K weekly downloads

## Recent versions

- 0.9.1 (latest) — 2026-08-24
- 0.9.0 — 2026-07-16
- 0.8.1 — 2025-04-10
- 0.8.0 — 2024-07-30
- 0.7.0 — 2021-02-22
- 0.6.0 — 2020-07-27
- 0.5.0 — 2019-05-10
- 0.4.2 — 2019-05-09
- 0.4.1 — 2019-05-09
- 0.4.0 — 2019-05-08
- 0.3.0 — 2019-02-18
- 0.2.0 — 2018-11-26
- 0.1.0 — 2017-09-12

## README

# isotropic-natural-sort

[![npm version](https://img.shields.io/npm/v/isotropic-natural-sort.svg)](https://www.npmjs.com/package/isotropic-natural-sort)
[![License](https://img.shields.io/npm/l/isotropic-natural-sort.svg)](https://github.com/ibi-group/isotropic-natural-sort/blob/main/LICENSE)
![](https://img.shields.io/badge/tests-passing-brightgreen.svg)
![](https://img.shields.io/badge/coverage-100%25-brightgreen.svg)

A configurable natural sorting implementation for JavaScript that intelligently sorts strings containing numbers, lexicographic orderings, and special characters.

## Why Use This?

- **Smarter Than Standard Sorting**: Sorts numerically within strings (e.g., "file2" comes before "file10")
- **Highly Configurable**: Customize case sensitivity, direction, special character handling, and more
- **Handles Mixed Content**: Works properly with arrays containing both numbers and strings
- **Lexicographic Ordering Support**: Properly sorts version numbers and IP addresses
- **Prefix Positioning**: Control where items with specific prefixes appear in sorted results

## Installation

```bash
npm install isotropic-natural-sort
```

## Usage

```javascript
import _naturalSort from 'isotropic-natural-sort';

// Basic usage with default options
[
    'file10.txt',
    'file2.txt',
    'file1.txt'
].sort(_naturalSort());
// Result: ['file1.txt', 'file2.txt', 'file10.txt']

// With custom options
const _customSortFunction = _naturalSort({
    caseSensitive: true,
    direction: 'desc',
    ignoreSpecialCharacters: false,
    prefixPositions: {
        'important-': 'first',
        'archive-': 'last'
    }
});

myArray.sort(_customSortFunction);
```

## Configuration Options

The `naturalSort` function accepts an options object with the following properties:

| Option | Type | Default | Description |
|--------|------|---------|-------------|
| `caseSensitive` | Boolean | `false` | Whether to consider character case when sorting strings |
| `direction` | String | `'asc'` | Sort direction, either `'asc'` or `'desc'` |
| `ignoreSpecialCharacters` | Boolean | `true` | Whether to ignore special characters (accents, diacritics) when sorting |
| `prefixPositions` | Object | `{}` | Object mapping prefixes to positions (`'first'` or `'last'`) |

## Error Handling

Passing an invalid `direction`, or a `prefixPositions` value other than `'first'` or `'last'`, throws an error that reports the offending value.

[isotropic-error](https://www.npmjs.com/package/isotropic-error) is an optional enhancement rather than a required dependency. When it is installed, validation errors are created with it, so the offending value is carried in the error's `details` and rendered into the stack trace. When it is not installed, the comparator falls back to the built-in `Error` with the same `details` attached as a property:

```javascript
import _naturalSort from 'isotropic-natural-sort';

try {
    _naturalSort({
        direction: 'sideways'
    });
} catch (error) {
    error.message; // 'Invalid direction'
    error.details; // { direction: 'sideways' }
}
```

## Examples

### Basic Sorting

```javascript
import _naturalSort from 'isotropic-natural-sort';

// Natural sorting of filenames
const _files = [
  'file10.txt',
  'file2.txt',
  'file1.txt',
  'file20.txt'
];

_files.sort(_naturalSort());
// Result: ['file1.txt', 'file2.txt', 'file10.txt', 'file20.txt']
```

### Case-Sensitive Sorting

```javascript
import _naturalSort from 'isotropic-natural-sort';

const _items = [
    'B',
    'a',
    'C',
    'b'
];

// Case-insensitive (default)
_items.sort(_naturalSort());
// Result: ['a', 'B', 'b', 'C']

// Case-sensitive
_items.sort(_naturalSort({
    caseSensitive: true
}));
// Result: ['B', 'C', 'a', 'b']
```

### Descending Order

```javascript
import _naturalSort from 'isotropic-natural-sort';

const _numbers = [
    1,
    10,
    2,
    20
];

_numbers.sort(_naturalSort({
    direction: 'desc'
}));
// Result: [20, 10, 2, 1]
```

### Special Character Handling

```javascript
import _naturalSort from 'isotropic-natural-sort';

const _names = [
    'cáfé charlotte',
    'cafe blue',
    'café amanda'
];

// Ignoring special characters (default)
_names.sort(_naturalSort());
// Result: ['café amanda', 'cafe blue', 'cáfé charlotte']

// Considering special characters
_names.sort(_naturalSort({
    ignoreSpecialCharacters: false
}));
// Result order depends on Unicode points of the accented characters
// Result: ['cafe blue', 'café amanda', 'cáfé charlotte']
```

### Lexicographic Ordering (IP Addresses, Versions)

```javascript
import _naturalSort from 'isotropic-natural-sort';

const _ips = [
        '192.168.0.10',
        '192.168.0.2',
        '10.0.0.1',
        '192.168.1.1'
    ],
    _versions = [
        '1.10.0',
        '1.2.0',
        '1.1.0',
        '1.2.1'
    ];

_ips.sort(_naturalSort());
// Result: ['10.0.0.1', '192.168.0.2', '192.168.0.10', '192.168.1.1']

_versions.sort(_naturalSort());
// Result: ['1.1.0', '1.2.0', '1.2.1', '1.10.0']
```

### Prefix Positioning

```javascript
import _naturalSort from 'isotropic-natural-sort';

const _tasks = [
    'normal-task3',
    'urgent-task1',
    'normal-task1',
    'low-priority-task',
    'urgent-task2'
];

_tasks.sort(_naturalSort({
    prefixPositions: {
        'low-priority-': 'last',
        'urgent-': 'first'
    }
}));
// Result: ['urgent-task1', 'urgent-task2', 'normal-task1', 'normal-task3', 'low-priority-task']
```

### Mixed Content

```javascript
import _naturalSort from 'isotropic-natural-sort';

const _mixed = [
    5,
    '10',
    'item3',
    2,
    'item10',
    'item1'
];

_mixed.sort(_naturalSort());
// Result: [2, 5, '10', 'item1', 'item3', 'item10']
```

## Handling Falsy Values

By default, `null`, `undefined`, and `NaN` values are sorted to the end of the array:

```javascript
import _naturalSort from 'isotropic-natural-sort';

const _withFalsy = [
    'b',
    null,
    'a',
    undefined,
    'c'
];

_withFalsy.sort(_naturalSort());
// Result: ['a', 'b', 'c', null, undefined]
```

## Comparison with Intl.Collator

The built-in [`Intl.Collator`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/Collator) with the `numeric` option also performs numeric-aware comparison and is a strong choice when locale-aware collation is the priority:

```javascript
const _collator = new Intl.Collator(undefined, {
    numeric: true,
    sensitivity: 'base'
});

['file10', 'file2'].sort(_collator.compare);
// Result: ['file2', 'file10']
```

For many common cases the two agree: both order `file2` before `file10`, and both sort dotted version or IP-address strings such as `1.2.9`, `1.2.10`, and `1.10.0` in the expected order. `Intl.Collator` is locale-aware, natively implemented, and covers a broad range of scripts.

isotropic-natural-sort is the better fit when you need behavior `Intl.Collator` does not offer:

- **Prefix positioning** through the `prefixPositions` option, to force strings with a given prefix to the start or end of the order.
- **Falsy-value handling**: `null`, `undefined`, and `NaN` are sorted to the end. `Intl.Collator.prototype.compare` coerces its arguments to strings, so `null` is compared as the string `'null'` and sorts among the `n` values rather than last.
- **Locale-independent, deterministic ordering**: results do not vary with the runtime's default locale.

```javascript
import _naturalSort from 'isotropic-natural-sort';

const _collator = new Intl.Collator(undefined, {
    numeric: true
});

['zebra', null, 'apple'].sort(_collator.compare);
// Result: ['apple', 'null', 'zebra'] (null coerced to a string)

['zebra', null, 'apple'].sort(_naturalSort());
// Result: ['apple', 'zebra', null] (null sorted last)
```

## Contributing

Please refer to [CONTRIBUTING.md](https://github.com/ibi-group/isotropic-natural-sort/blob/main/CONTRIBUTING.md) for contribution guidelines.

## Issues

If you encounter any issues, please file them at https://github.com/ibi-group/isotropic-natural-sort/issues

---
_Source: https://npm.io/package/isotropic-natural-sort · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
