Security News
The Push to Ban Ransom Payments Is Gaining Momentum
Ransomware costs victims an estimated $30 billion per year and has gotten so out of control that global support for banning payments is gaining momentum.
natural-compare-lite
Advanced tools
Package description
The natural-compare-lite npm package is designed for comparing strings in a way that humans might naturally order them. This is particularly useful when sorting lists of strings that include numbers, as it ensures that numerical parts are compared numerically rather than lexicographically. For example, 'item2' will come before 'item10', which is not the case with standard string comparison.
Natural String Comparison
This feature allows for the natural comparison of strings, especially useful for sorting arrays where elements contain numbers. The provided code demonstrates sorting an array of strings in a natural order.
"use strict";\nconst naturalCompare = require('natural-compare-lite');\nconsole.log(['item10', 'item2'].sort(naturalCompare)); // ['item2', 'item10']
This package offers functionality similar to natural-compare-lite, providing natural string comparison capabilities. It differs in implementation details and possibly in performance characteristics, but serves a similar purpose of comparing strings in a human-friendly way.
Alphanum-sort is another package that provides natural string sorting capabilities. It allows for additional customization options such as case sensitivity and whether to treat whitespace and punctuation as part of the sorting criteria. This makes it a versatile alternative to natural-compare-lite, depending on the specific needs of a project.
Readme
@version 0.4.6
@date 2014-01-27
@stability 2 - Unstable
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.
Download compressed (310 bytes, 236 bytes gzipped) or uncompressed source.
<script src=min.natural-compare.js></script>
npm install natural-compare-lite
require("natural-compare-lite")
// 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 comparision 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);
})
Copyright (c) 2012, 2014 Lauri Rooden <lauri@rooden.ee>
The MIT License
FAQs
Compare strings containing a mix of letters and numbers in the way a human being would in sort order.
We found that natural-compare-lite demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Ransomware costs victims an estimated $30 billion per year and has gotten so out of control that global support for banning payments is gaining momentum.
Application Security
New SEC disclosure rules aim to enforce timely cyber incident reporting, but fear of job loss and inadequate resources lead to significant underreporting.
Security News
The Python Software Foundation has secured a 5-year sponsorship from Fastly that supports PSF's activities and events, most notably the security and reliability of the Python Package Index (PyPI).