• Stars
    star
    108
  • Rank 321,259 (Top 7 %)
  • Language
    JavaScript
  • Created over 11 years ago
  • Updated 10 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Compare strings containing a mix of letters and numbers in the way a human being would in sort order.

Natural Compare – Coverage size Buy Me A Tea

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".

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
<script src=natural-compare.js></script>
  • In node.js: npm install natural-compare-lite
var naturalCompare = require("natural-compare-lite")

Usage

// 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.

// 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 | npm package

Licence

Copyright (c) 2012-2022 Lauri Rooden <[email protected]>
The MIT License

More Repositories

1

json-lite

Fast non-blocking JSON viewer for Chrome and Firefox
JavaScript
195
star
2

date-format-lite

Format, parse and manipulate dates in JavaScript
JavaScript
42
star
3

dom

Dependency-free DOM library for handling HTML files on server-side
JavaScript
24
star
4

uri-template-lite

URI Template RFC 6570 expansion and extraction
JavaScript
19
star
5

litejs

Full-stack web framework in a tiny package
JavaScript
18
star
6

browser-cookie-lite

Cookie getter/setter for browser in 340 bytes
JavaScript
17
star
7

browser-upgrade-lite

DEPRECATED! It is merged into @litejs/ui/polyfill
JavaScript
10
star
8

crypto-lite

Standard and secure cryptographic algorithms for browser
JavaScript
5
star
9

selector-lite

DEPRECATED! It is merged into dom-lite
JavaScript
4
star
10

cli

Command line tools for LiteJS framework
JavaScript
4
star
11

s3

Dependency-free S3 client when full SDK is not needed
JavaScript
3
star
12

ui

UI engine for LiteJS full-stack framework
JavaScript
3
star
13

zip

Dependency-free library for creating ZIP files in the browser
JavaScript
2
star
14

json-util

JSON Pointer [RFC 6901] and JSON Merge Patch [RFC 7396] implementation
JavaScript
2
star
15

events-lite

DEPRECATED! It is merged into litejs
JavaScript
1
star
16

elements-lite

DEPRECATED! It is merged into litejs, use require('litejs/el')
JavaScript
1
star
17

browser-event-lite

DEPRECATED! It is merged into litejs
JavaScript
1
star
18

liquid-lite

DEPRECATED! It was fun but string based template engine is no-go
JavaScript
1
star
19

liquid-filters-lite

DEPRECATED! Standard Filters for liquid-lite extending native prototypes
JavaScript
1
star