alphanum-sort
Alphanumeric sorting algorithm
Last updated 3 months ago by GitHub Actions .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ npm install alphanum-sort 
SYNC missed versions from official npm registry.

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

Current Tags

  • 1.1.0                                ...           latest (3 months ago)

4 Versions

  • 1.1.0                                ...           3 months ago
  • 1.0.2                                ...           11 years ago
  • 1.0.1                                ...           11 years ago
  • 1.0.0                                ...           11 years ago
Maintainers (1)
Downloads
Total 10
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 3
Dependencies (0)
None
Dev Dependencies (1)

© 2010 - cnpmjs.org x YWFE | Home | YWFE