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