alphanum-sort
v1.1.0
Published
Alphanumeric sorting algorithm
Maintainers
Readme
alphanum-sort
Fast, natural-order sorting for strings and numbers.
Unlike lexicographic sorting, alphanum-sort compares numeric parts by value, so
item2 comes before item10. It supports mixed values, leading zeros, signs,
and case-insensitive comparisons without modifying the input array.
Install
npm install alphanum-sortUsage
var sort = require('alphanum-sort');
var items = ['item20', 'item19', 'item1', 'item10', 'item2'];
var result = sort(items);
console.log(result);
// ['item1', 'item2', 'item10', 'item19', 'item20']Case-insensitive sorting
sort(['A', 'C', 'E', 'b', 'd', 'f'], { insensitive: true });
// ['A', 'b', 'C', 'd', 'E', 'f']Signed numbers
sort(['10', '-1', '5', '-20'], { sign: true });
// ['-20', '-1', '5', '10']API
sort(array[, options])
Returns a sorted copy of array. Values are compared as strings but retain
their original types in the returned array.
options.insensitive
- Type:
Boolean - Default:
false
Compare values without regard to letter case.
options.sign
- Type:
Boolean - Default:
false
Treat + and - immediately before digits as numeric signs.
Development
npm install
npm testThe test suite supports Node.js 0.10 and newer.
