Skip to content

discoveryjs/natural-compare

Repository files navigation

NPM version Build Status Coverage Status Minified size Minified + gzip size

@discoveryjs/natural-compare

Compare strings in a natural order

Install

npm install @discoveryjs/natural-compare

Usage

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>

License

MIT