![Oracle Drags Its Feet in the JavaScript Trademark Dispute](https://cdn.sanity.io/images/cgdhsj6q/production/919c3b22c24f93884c548d60cbb338e819ff2435-1024x1024.webp?w=400&fit=max&auto=format)
Security News
Oracle Drags Its Feet in the JavaScript Trademark Dispute
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
@discoveryjs/natural-compare
Advanced tools
@discoveryjs/natural-compare is an npm package that provides a natural order string comparison algorithm. This is particularly useful for sorting strings in a way that humans would expect, such as sorting file names, version numbers, or any other strings that contain numbers.
Basic Natural Comparison
This feature allows you to sort an array of strings in a natural order. In the example, file names are sorted in a way that a human would expect, with 'file10.txt' coming after 'file2.txt'.
const naturalCompare = require('@discoveryjs/natural-compare');
const arr = ['file10.txt', 'file2.txt', 'file1.txt'];
arr.sort(naturalCompare);
console.log(arr); // Output: ['file1.txt', 'file2.txt', 'file10.txt']
Case Insensitive Comparison
This feature allows you to perform a case-insensitive natural comparison. By converting all strings to lowercase before comparison, the sorting ignores case differences.
const naturalCompare = require('@discoveryjs/natural-compare');
const arr = ['File10.txt', 'file2.txt', 'file1.txt'];
arr.sort((a, b) => naturalCompare(a.toLowerCase(), b.toLowerCase()));
console.log(arr); // Output: ['file1.txt', 'file2.txt', 'File10.txt']
natural-orderby is another npm package that provides natural sorting for arrays of objects and strings. It offers more flexibility by allowing you to specify the property to sort by in an array of objects. Compared to @discoveryjs/natural-compare, it is more feature-rich but also more complex to use.
string-natural-compare is a lightweight npm package for natural string comparison. It is similar to @discoveryjs/natural-compare in terms of functionality but is less popular and has fewer features. It is a good alternative if you need a simple and small library for natural string comparison.
Compare strings in a natural order
npm install @discoveryjs/natural-compare
import { naturalCompare } from '@discoveryjs/natural-compare';
[
'file10.js',
'file1.js',
'file2.js',
'file12.js',
'file9.js'
].sort(naturalCompare);
// file1.js
// file2.js
// file9.js
// file10.js
// file12.js
// standart sorting
[
'file10.js',
'file1.js',
'file2.js',
'file12.js',
'file9.js'
].sort();
// file1.js
// file10.js
// file12.js
// file2.js
// file9.js
In browser:
<!-- ESM -->
<script type="module">
import { natualCompare } from "@discoveryjs/natural-compare/dist/natural-compare.esm.js";
array.sort(naturalCompare);
</script>
<!-- Old way -->
<script src="@discoveryjs/natural-compare/dist/natural-compare.js"></script>
<script>
array.sort(naturalCompare);
</script>
MIT
FAQs
Compare strings in a natural order
We found that @discoveryjs/natural-compare demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 3 open source maintainers 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
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
Security News
The Linux Foundation is warning open source developers that compliance with global sanctions is mandatory, highlighting legal risks and restrictions on contributions.
Security News
Maven Central now validates Sigstore signatures, making it easier for developers to verify the provenance of Java packages.