# natural-compare-lite

> Compare strings in the way a human would in sort

Latest version **1.4.1** (published 2026-09-02) · MIT license · 0 weekly downloads

## Install

```sh
npm install natural-compare-lite
pnpm add natural-compare-lite
yarn add natural-compare-lite
bun add natural-compare-lite
```

## Health

**Score 68/100 (B)** — status: active.

Positive: has types package; no vulnerabilities; has provenance; recently updated; high maintenance score; high quality score.

Warnings: low downloads; no esm support.

## Facts

| | |
|---|---|
| Version | 1.4.1 |
| Published | 2026-09-02 |
| First published | 2013-04-01 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | separate (@types/natural-compare-lite) |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 14.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Provenance | attested (GitHub Actions) |
| GitHub stars | 113 |
| Author | Lauri Rooden |
| Maintainers | lauriro |
| Keywords | alphanum, compare, natcmp, natsort, natural, order, sort, litejs |

## Links

- npm: https://www.npmjs.com/package/natural-compare-lite
- Repository: https://github.com/litejs/natural-compare-lite
- Homepage: https://litejs.com
- Issues: https://github.com/litejs/natural-compare-lite/issues
- npm.io page: https://npm.io/package/natural-compare-lite

## Recent versions

- 1.4.1 (latest) — 2026-09-02
- 1.4.0 — 2015-10-26
- 1.3.1 — 2015-03-02
- 1.3.0 — 2014-11-29
- 1.2.2 — 2014-10-02
- 1.2.0 — 2014-10-02
- 1.1.0 — 2014-09-30
- 1.0.0 — 2014-05-15
- 0.4.7 — 2014-05-03
- 0.4.6 — 2014-01-27
- 0.4.5 — 2014-01-21
- 0.4.4 — 2013-11-28
- 0.4.3 — 2013-10-09
- 0.4.2 — 2013-10-08
- 0.4.1 — 2013-10-08
- … 8 more at https://npm.io/package/natural-compare-lite/versions

## README

[1]: https://badgen.net/coveralls/c/github/litejs/natural-compare-lite
[2]: https://coveralls.io/r/litejs/natural-compare-lite
[3]: https://badgen.net/packagephobia/install/natural-compare-lite
[4]: https://packagephobia.now.sh/result?p=natural-compare-lite
[5]: https://badgen.net/badge/icon/Buy%20Me%20A%20Tea/orange?icon=kofi&label
[6]: https://www.buymeacoffee.com/lauriro



Natural Compare &ndash;  [![Coverage][1]][2] [![size][3]][4] [![Buy Me A Tea][5]][6]
===============

Compare strings containing a mix of letters and numbers
in the way a human being would in sort order.
This is described as a "natural ordering".

```text
Standard sorting:   Natural order sorting:
    img1.png            img1.png
    img10.png           img2.png
    img12.png           img10.png
    img2.png            img12.png
```

String.naturalCompare returns a number indicating
whether a reference string comes before or after or is the same
as the given string in sort order.
Use it with builtin sort() function.



### Installation

- In browser

```html
<script src=natural-compare.js></script>
```

- In node.js: `npm install natural-compare-lite`

```javascript
var naturalCompare = require("natural-compare-lite")
```

### Usage

```javascript
// Simple case sensitive example
var a = ["z1.doc", "z10.doc", "z17.doc", "z2.doc", "z23.doc", "z3.doc"];
a.sort(String.naturalCompare);
// ["z1.doc", "z2.doc", "z3.doc", "z10.doc", "z17.doc", "z23.doc"]

// Use wrapper function for case insensitivity
a.sort(function(a, b){
  return String.naturalCompare(a.toLowerCase(), b.toLowerCase());
})

// In most cases we want to sort an array of objects
var a = [ {"street":"350 5th Ave", "room":"A-1021"}
        , {"street":"350 5th Ave", "room":"A-21046-b"} ];

// sort by street, then by room
a.sort(function(a, b){
  return String.naturalCompare(a.street, b.street) || String.naturalCompare(a.room, b.room);
})

// When text transformation is needed (eg toLowerCase()),
// it is best for performance to keep
// transformed key in that object.
// There are no need to do text transformation
// on each comparison when sorting.
var a = [ {"make":"Audi", "model":"A6"}
        , {"make":"Kia",  "model":"Rio"} ];

// sort by make, then by model
a.map(function(car){
  car.sort_key = (car.make + " " + car.model).toLowerCase();
})
a.sort(function(a, b){
  return String.naturalCompare(a.sort_key, b.sort_key);
})
```

- Works well with dates in ISO format eg "Rev 2012-07-26.doc".


### Custom alphabet

It is possible to configure a custom alphabet
to achieve a desired order.

```javascript
// Estonian alphabet
String.alphabet = "ABDEFGHIJKLMNOPRSŠZŽTUVÕÄÖÜXYabdefghijklmnoprsšzžtuvõäöüxy"
["t", "z", "x", "õ"].sort(String.naturalCompare)
// ["z", "t", "õ", "x"]

// Russian alphabet
String.alphabet = "АБВГДЕЁЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯабвгдеёжзийклмнопрстуфхцчшщъыьэюя"
["Ё", "А", "Б"].sort(String.naturalCompare)
// ["А", "Б", "Ё"]
```


## External links

[GitHub repo](https://github.com/litejs/natural-compare-lite) |
[npm package](https://npmjs.org/package/natural-compare-lite)


## Licence

Copyright (c) 2012-2026 Lauri Rooden &lt;lauri@rooden.ee&gt;  
[The MIT License](http://lauri.rooden.ee/mit-license.txt)

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