Home
Docs
GitHub
Pricing
Blog
Log In

Run Sandworm Audit for your App

Get started
Hold on, we're currently generating a fresh version of this report
Generated on May 8, 2024 via pnpm

natural-compare 1.4.0

Compare strings containing a mix of letters and numbers in the way a human being would in sort order.
Package summary
Share
0
issues
1
license
1
MIT
Package created
18 Nov 2014
Version published
22 Jul 2016
Maintainers
1
Total deps
1
Direct deps
0
License
MIT

Issues

0
This package has no issues

Licenses

MIT License

Permissive
OSI Approved
This is a human-readable summary of (and not a substitute for) the license. Disclaimer.
Can
commercial-use
modify
distribute
sublicense
private-use
Cannot
hold-liable
Must
include-copyright
include-license
1 Packages, Including:
natural-compare@1.4.0
Disclaimer

This deed highlights only some of the key features and terms of the actual license. It is not a license and has no legal value. You should carefully review all of the terms and conditions of the actual license before using the licensed material.

Sandworm is not a law firm and does not provide legal services. Distributing, displaying, or linking to this deed or the license that it summarizes does not create a lawyer-client or any other relationship.

Direct Dependencies

0
All Dependencies CSV
ⓘ This is a list of natural-compare 's direct dependencies. Data on all dependencies, including transitive ones, is available via CSV download.
NameVersionSizeLicenseTypeVulnerabilities

Visualizations

Frequently Asked Questions

What does natural-compare do?

Natural-compare is a powerful JavaScript tool that allows developers to compare strings containing a mix of letters and numbers in the way a human being would in sort order, described as a "natural ordering". It helps to ensure your data is sorted in a way that makes intuitive sense to a human reader, making your application's user interface smoother and more user-friendly.

How do you use natural-compare?

One of the advantages of natural-compare is its ease of use. If you want to use this tool in the browser, simply include the following line in your HTML script tag:

<script src=min.natural-compare.js></script>

For use in node.js you can install it via npm with the following command:

npm install natural-compare-lite

and then require the package in your JavaScript file with:

require("natural-compare-lite")

Once the set-up is complete, you can start sorting strings in a naturally ordered way.

var a = ["z1.doc", "z10.doc", "z17.doc", "z2.doc", "z23.doc", "z3.doc"];
a.sort(String.naturalCompare);
// This will output
// ["z1.doc", "z2.doc", "z3.doc", "z10.doc", "z17.doc", "z23.doc"]

Case-insensitive sorting can be achieved via a wrapper function:

a.sort(function(a, b){
  return String.naturalCompare(a.toLowerCase(), b.toLowerCase());
});

It also works well with arrays of objects, and allows for additional capabilities like sorting by multiple properties, and pre-transforming keys for more efficient comparison:

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);
})

This tool also supports international languages and alphabets:

// Russian alphabet
String.alphabet = "АБВГДЕЁЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯабвгдеёжзийклмнопрстуфхцчшщъыьэюя"
["Ё", "А", "Б"].sort(String.naturalCompare)
// This will output
// ["А", "Б", "Ё"]

Where are the natural-compare docs?

The documentation of natural-compare can be found in its GitHub repository: https://github.com/litejs/natural-compare-lite. Here, you'll find examples of how to use the tool, various edge cases and caveats, as well as guidelines on how to contribute to its development.

All Versions