Gathering detailed insights and metrics for string-natural-compare
Gathering detailed insights and metrics for string-natural-compare
Gathering detailed insights and metrics for string-natural-compare
Gathering detailed insights and metrics for string-natural-compare
natural-compare
Compare strings containing a mix of letters and numbers in the way a human being would in sort order.
natural-compare-lite
Compare strings containing a mix of letters and numbers in the way a human being would in sort order.
@types/string-natural-compare
TypeScript definitions for string-natural-compare
natural-orderby
Lightweight and performant natural sorting of arrays and collections by differentiating between unicode characters, numbers, dates, etc.
Compare alphanumeric strings the same way a human would, using a natural order algorithm.
npm install string-natural-compare
Typescript
Module System
Node Version
NPM Version
JavaScript (100%)
Total Downloads
774,487,434
Last Day
689,960
Last Week
4,290,603
Last Month
17,986,481
Last Year
213,717,428
MIT License
49 Stars
120 Commits
5 Forks
1 Watchers
1 Branches
2 Contributors
Updated on Oct 18, 2024
Minified
Minified + Gzipped
Latest Version
3.0.1
Package Id
string-natural-compare@3.0.1
Size
4.00 kB
NPM Version
6.13.4
Node Version
13.6.0
Published on
Jan 21, 2020
Cumulative downloads
Total Downloads
Last Day
14.6%
689,960
Compared to previous day
Last Week
9.3%
4,290,603
Compared to previous week
Last Month
-7.9%
17,986,481
Compared to previous month
Last Year
1.4%
213,717,428
Compared to previous year
Compare alphanumeric strings the same way a human would, using a natural order algorithm (originally known as the alphanum algorithm) where numeric characters are sorted based on their numeric values rather than their ASCII values.
Standard sorting: Natural order sorting:
img1.png img1.png
img10.png img2.png
img12.png img10.png
img2.png img12.png
This module exports a function that returns a number indicating whether one string should come before, after, or is the same as another string.
It can be used directly with the native .sort()
array method.
This module can compare strings containing any size of number and is heavily tested with a custom benchmark suite to make sure that it is as fast as possible.
1npm install string-natural-compare --save 2# or 3yarn add string-natural-compare
naturalCompare(strA, strB[, options])
strA
(string)strB
(string)options
(object) - Optional options object with the following options:
caseInsensitive
(boolean) - Set to true
to compare strings case-insensitively. Default: false
.alphabet
(string) - A string of characters that define a custom character ordering. Default: undefined
.1const naturalCompare = require('string-natural-compare'); 2 3// Simple, case-sensitive sorting 4const files = ['z1.doc', 'z10.doc', 'z17.doc', 'z2.doc', 'z23.doc', 'z3.doc']; 5files.sort(naturalCompare); 6// -> ['z1.doc', 'z2.doc', 'z3.doc', 'z10.doc', 'z17.doc', 'z23.doc'] 7 8 9// Case-insensitive sorting 10const chars = ['B', 'C', 'a', 'd']; 11const naturalCompareCI = (a, b) => naturalCompare(a, b, {caseInsensitive: true}); 12chars.sort(naturalCompareCI); 13// -> ['a', 'B', 'C', 'd'] 14 15// Note: 16['a', 'A'].sort(naturalCompareCI); // -> ['a', 'A'] 17['A', 'a'].sort(naturalCompareCI); // -> ['A', 'a'] 18 19 20// Compare strings containing large numbers 21naturalCompare( 22 '1165874568735487968325787328996865', 23 '265812277985321589735871687040841' 24); 25// -> 1 26// (Other inputs with the same ordering as this example may yield a different number > 0) 27 28 29// Sorting an array of objects 30const hotelRooms = [ 31 {street: '350 5th Ave', room: 'A-1021'}, 32 {street: '350 5th Ave', room: 'A-21046-b'} 33]; 34// Sort by street (case-insensitive), then by room (case-sensitive) 35hotelRooms.sort((a, b) => ( 36 naturalCompare(a.street, b.street, {caseInsensitive: true}) || 37 naturalCompare(a.room, b.room) 38)); 39 40 41// When text transformation is needed or when doing a case-insensitive sort on a 42// large array of objects, it is best for performance to pre-compute the 43// transformed text and store it on the object. This way, the text will not need 44// to be transformed for every comparison while sorting. 45const cars = [ 46 {make: 'Audi', model: 'R8'}, 47 {make: 'Porsche', model: '911 Turbo S'} 48]; 49// Sort by make, then by model (both case-insensitive) 50for (const car of cars) { 51 car.sortKey = (car.make + ' ' + car.model).toLowerCase(); 52} 53cars.sort((a, b) => naturalCompare(a.sortKey, b.sortKey)); 54 55 56// Using a custom alphabet (Russian alphabet) 57const russianOpts = { 58 alphabet: 'АБВГДЕЁЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯабвгдеёжзийклмнопрстуфхцчшщъыьэюя', 59}; 60['Ё', 'А', 'б', 'Б'].sort((a, b) => naturalCompare(a, b, russianOpts)); 61// -> ['А', 'Б', 'Ё', 'б']
Note: Putting numbers in the custom alphabet can cause undefined behaviour.
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
no dangerous workflow patterns detected
Reason
license file detected
Details
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
Found 1/30 approved changesets -- score normalized to 0
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Reason
22 existing vulnerabilities detected
Details
Score
Last Scanned on 2025-05-05
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More