https://github.com/litejs/natural-compare-lite
Compare strings containing a mix of letters and numbers in the way a human being would in sort order.
https://github.com/litejs/natural-compare-lite
Last synced: about 1 year ago
JSON representation
Compare strings containing a mix of letters and numbers in the way a human being would in sort order.
- Host: GitHub
- URL: https://github.com/litejs/natural-compare-lite
- Owner: litejs
- License: mit
- Created: 2013-04-01T12:23:47.000Z (about 13 years ago)
- Default Branch: main
- Last Pushed: 2024-10-11T10:25:44.000Z (over 1 year ago)
- Last Synced: 2025-05-06T07:06:01.984Z (about 1 year ago)
- Language: JavaScript
- Homepage:
- Size: 54.7 KB
- Stars: 112
- Watchers: 4
- Forks: 7
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
- Security: .github/SECURITY.md
Awesome Lists containing this project
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 – [![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
```
- 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-2022 Lauri Rooden <lauri@rooden.ee>
[The MIT License](http://lauri.rooden.ee/mit-license.txt)