JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 4498380
  • Score
    100M100P100Q216142F
  • License MIT

Compare alphanumeric strings the same way a human would, using a natural order algorithm

Package Exports

  • string-natural-compare

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (string-natural-compare) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

String Natural Compare

Compare alphanumeric strings the same way a human would, using a natural order algorithm

NPM Version Bower Version Build Status Coverage Status devDependency Status

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

This module makes two functions available on the global String object:

  • String.naturalCompare (case-sensitive)
  • String.naturalCaseCompare (case-insensitive)

These functions return a number indicating whether one string should come before, after, or is the same as another string. They can be easily used with the native .sort() array method.

Fast and Robust

This module uses an extremely performant and robust algorithm to compare alphanumeric strings. It does not convert numeric substrings into JavaScript numbers, so it can compare strings containing very large numeric substrings (i.e. exceeding what can be contained in a 64-bit integer). The algorithm has been optimized to be very fast, even when a custom alphabet has been configured.

Installation

Node.js:

npm install string-natural-compare --save

Then in your JS:

require('string-natural-compare');

Bower:

bower install string-natural-compare

Include the script in your HTML (drop the ".min" to use the development version):

<script src="/bower_components/string-natural-compare/dist/natural-compare.min.js"></script>

Note: IE8 and lower not supported.

Download:

Production and development versions can be found here and can be included in your page similar to the HTML example above.

Usage

// Simple case-sensitive sorting
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']


// Simple case-insensitive sorting
var a = ['B', 'C', 'a', 'd'];
a.sort(String.naturalCaseCompare);
// -> ['a', 'B', 'C', 'd']


// Compare very large numbers as strings (or strings containing long, numeric substrings)
String.naturalCompare(
  '1165874568735487968325787328996865',
  '1165874568735487968325787328996864'
);
// -> 1


// 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 or when doing a case-insensitive sort on a
// large array, it is best for performance to pre-compute the transformed text
// and store it in that object. This way, the text transformation will not be
// needed for every comparison when sorting.
var a = [
  {make: 'Audi', model: 'R8'},
  {make: 'Porsche', model: '911 Turbo S'}
];

// Sort by make, then by model
a = 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);
});

Custom Alphabet

It is possible to configure a custom alphabet to achieve a desired character ordering.

// 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);
// -> ['А', 'Б', 'Ё']

Note: Putting numbers in the custom alphabet can cause undefined behaviour.