Want to see pretty graphs? Log in now!
npm install alphanumeric-sort
|2||downloads in the last week|
|6||downloads in the last month|
|Last Published By|
|Version||0.0.1 last updated 6 months ago|
|Keywords||alphanumeric, natural, sort|
A usable, portable, algorithm/comparator for sorting things like filenames and addresses that contain numbers.
The algorithm will sort the following files in this order:
file 1.txt file 5.txt file 39.txt file 100.txt
file 1.txt file 100.txt file 39.txt file 5.txt
The library provides a comparator that can be fed to
First you do
> npm install alphanumeric-sort
And then you do
var compare = require('alphanumeric-sort').compare; ["file 100.txt", "file 5.txt", "file 39.txt"].sort(compare); //=> ["file 5.txt", "file 39.txt", "file 100.txt"]
The implementation is quick & dirty - it just prepends "enough" zeroes before every digit sequence in the string before comparing lexicographically.
The maximum amount of consecutive digits is 20, after that all bets are off.
It's not probably very fast. (To be honest, I haven't really tested.)