Gathering detailed insights and metrics for natural-compare-lite
Gathering detailed insights and metrics for natural-compare-lite
Gathering detailed insights and metrics for natural-compare-lite
Gathering detailed insights and metrics for natural-compare-lite
Compare strings containing a mix of letters and numbers in the way a human being would in sort order.
npm install natural-compare-lite
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
109 Stars
60 Commits
8 Forks
6 Watching
2 Branches
4 Contributors
Updated on 20 Nov 2024
JavaScript (100%)
Cumulative downloads
Total Downloads
Last day
-7.5%
1,740,865
Compared to previous day
Last week
1.5%
10,164,160
Compared to previous week
Last month
10.1%
41,981,840
Compared to previous month
Last year
-2.1%
489,471,730
Compared to previous year
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".
1Standard sorting: Natural order sorting: 2 img1.png img1.png 3 img10.png img2.png 4 img12.png img10.png 5 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.
1<script src=natural-compare.js></script>
npm install natural-compare-lite
1var naturalCompare = require("natural-compare-lite")
1// Simple case sensitive example 2var a = ["z1.doc", "z10.doc", "z17.doc", "z2.doc", "z23.doc", "z3.doc"]; 3a.sort(String.naturalCompare); 4// ["z1.doc", "z2.doc", "z3.doc", "z10.doc", "z17.doc", "z23.doc"] 5 6// Use wrapper function for case insensitivity 7a.sort(function(a, b){ 8 return String.naturalCompare(a.toLowerCase(), b.toLowerCase()); 9}) 10 11// In most cases we want to sort an array of objects 12var a = [ {"street":"350 5th Ave", "room":"A-1021"} 13 , {"street":"350 5th Ave", "room":"A-21046-b"} ]; 14 15// sort by street, then by room 16a.sort(function(a, b){ 17 return String.naturalCompare(a.street, b.street) || String.naturalCompare(a.room, b.room); 18}) 19 20// When text transformation is needed (eg toLowerCase()), 21// it is best for performance to keep 22// transformed key in that object. 23// There are no need to do text transformation 24// on each comparison when sorting. 25var a = [ {"make":"Audi", "model":"A6"} 26 , {"make":"Kia", "model":"Rio"} ]; 27 28// sort by make, then by model 29a.map(function(car){ 30 car.sort_key = (car.make + " " + car.model).toLowerCase(); 31}) 32a.sort(function(a, b){ 33 return String.naturalCompare(a.sort_key, b.sort_key); 34})
It is possible to configure a custom alphabet to achieve a desired order.
1// Estonian alphabet 2String.alphabet = "ABDEFGHIJKLMNOPRSŠZŽTUVÕÄÖÜXYabdefghijklmnoprsšzžtuvõäöüxy" 3["t", "z", "x", "õ"].sort(String.naturalCompare) 4// ["z", "t", "õ", "x"] 5 6// Russian alphabet 7String.alphabet = "АБВГДЕЁЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯабвгдеёжзийклмнопрстуфхцчшщъыьэюя" 8["Ё", "А", "Б"].sort(String.naturalCompare) 9// ["А", "Б", "Ё"]
Copyright (c) 2012-2022 Lauri Rooden <lauri@rooden.ee>
The MIT License
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
no dangerous workflow patterns detected
Reason
security policy file detected
Details
Reason
0 existing vulnerabilities detected
Reason
license file detected
Details
Reason
SAST tool detected but not run on all commits
Details
Reason
1 commit(s) and 1 issue activity found in the last 90 days -- score normalized to 1
Reason
Found 2/29 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
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Score
Last Scanned on 2024-11-18
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