npm.io
1.1.0 • Published 4 weeks ago

alphanum-sort

Licence
MIT
Version
1.1.0
Deps
0
Size
7 kB
Vulns
0
Weekly
0
Stars
29

alphanum-sort

CI

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-sort

Usage

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 test

The test suite supports Node.js 0.10 and newer.

License

MIT Bogdan Chadkin

Keywords