natural-compare

@version    1.4.0
@date       2015-10-26
@stability  3 - Stable

Natural Compare – Build Coverage

Compare strings containing a mix of letters and numbers in the way a human being would in sort order. This is described as a "natural ordering".

Standard sorting:   Natural order sorting:
    img1.png            img1.png
    img10.png           img2.png
    img12.png           img10.png
    img2.png            img12.png

String.naturalCompare returns a number indicating whether a reference string comes before or after or is the same as the given string in sort order. Use it with builtin sort() function.

Installation

  • In browser

<script src=min.natural-compare.js></script>
  • In node.js: npm install natural-compare-lite

require("natural-compare-lite")

Usage

  • Works well with dates in ISO format eg "Rev 2012-07-26.doc".

Custom alphabet

It is possible to configure a custom alphabet to achieve a desired order.

  • [GitHub repo][https://github.com/litejs/natural-compare-lite]

Licence

Copyright (c) 2012-2015 Lauri Rooden <lauri@rooden.ee> The MIT License

Last updated

Was this helpful?