{"name":"natural-compare","version":"1.2.2","license":"MIT","author":{"name":"Graeme Yeates","url":"https://github.com/megawac/natural-compare"},"description":"Compare strings containing a mix of letters and numbers in the way a human being would in sort order.","keywords":["string","natural","order","sort","natsort","natcmp","compare","alphanum"],"main":"index.js","scripts":{"travis-test":"istanbul cover ./tests/run.js && (coveralls < coverage/lcov.info || exit 0)","test":"node tests/run.js"},"repository":{"type":"git","url":"git://github.com/litejs/natural-compare.git"},"bugs":{"url":"https://github.com/litejs/natural-compare/issues"},"devDependencies":{"testman":"*"},"readme":"\n[Build]:    http://img.shields.io/travis/megawac/natural-compare.png\n[Coverage]: http://img.shields.io/coveralls/megawac/natural-compare.png\n[1]: https://travis-ci.org/megawac/natural-compare\n[2]: https://coveralls.io/r/megawac/natural-compare\n\n\nNatural Compare &ndash; [![Build][]][1] [![Coverage][]][2]\n===============\n\nCompare strings containing a mix of letters and numbers\nin the way a human being would in sort order.\nThis is described as a \"natural ordering\".\n\n```plain\nStandard sorting:   Natural order sorting:\n    img1.png            img1.png\n    img10.png           img2.png\n    img12.png           img10.png\n    img2.png            img12.png\n```\n\nString.naturalCompare returns a number indicating\nwhether a reference string comes before or after or is the same\nas the given string in sort order.\nUse it with builtin sort() function.\n\n\n\n### Installation\n\n- In browser\n\n```html\n<script src=min.natural-compare.js></script>\n```\n\n- In node.js: `npm install natural-compare`\n\n```javascript\nrequire(\"natural-compare-lite\")\n```\n\n### Usage\n\n```javascript\n// Simple case sensitive example\nvar a = [\"z1.doc\", \"z10.doc\", \"z17.doc\", \"z2.doc\", \"z23.doc\", \"z3.doc\"];\na.sort(String.naturalCompare);\n// [\"z1.doc\", \"z2.doc\", \"z3.doc\", \"z10.doc\", \"z17.doc\", \"z23.doc\"]\n\n// Use wrapper function for case insensitivity\na.sort(function(a, b){\n  return String.naturalCompare(a.toLowerCase(), b.toLowerCase());\n})\n\n// In most cases we want to sort an array of objects\nvar a = [ {\"street\":\"350 5th Ave\", \"room\":\"A-1021\"}\n        , {\"street\":\"350 5th Ave\", \"room\":\"A-21046-b\"} ];\n\n// sort by street, then by room\na.sort(function(a, b){\n  return String.naturalCompare(a.street, b.street) || String.naturalCompare(a.room, b.room);\n})\n\n// When text transformation is needed (eg toLowerCase()),\n// it is best for performance to keep\n// transformed key in that object. \n// There are no need to do text transformation\n// on each comparision when sorting.\nvar a = [ {\"make\":\"Audi\", \"model\":\"A6\"}\n        , {\"make\":\"Kia\",  \"model\":\"Rio\"} ];\n\n// sort by make, then by model\na.map(function(car){\n  car.sort_key = (car.make + \" \" + car.model).toLowerCase();\n})\na.sort(function(a, b){\n  return String.naturalCompare(a.sort_key, b.sort_key);\n})\n```\n\n- Works well with dates in ISO format eg \"Rev 2012-07-26.doc\".\n\n\n### Custom alphabet\n\nIt is possible to configure a custom alphabet\nto achieve a desired order.\n\n```javascript\n// Estonian alphabet\nString.alphabet = \"ABDEFGHIJKLMNOPRSŠZŽTUVÕÄÖÜXYabdefghijklmnoprsšzžtuvõäöüxy\"\n[\"t\", \"z\", \"x\", \"õ\"].sort(String.naturalCompare)\n// [\"z\", \"t\", \"õ\", \"x\"]\n\n// Russian alphabet\nString.alphabet = \"АБВГДЕЁЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯабвгдеёжзийклмнопрстуфхцчшщъыьэюя\"\n[\"Ё\", \"А\", \"Б\"].sort(String.naturalCompare)\n// [\"А\", \"Б\", \"Ё\"]\n```\n\n\nExternal links\n--------------\n\n-   [Source-code on Github](https://github.com/litejs/natural-compare-lite)\n-   [Package on npm](https://npmjs.org/package/natural-compare-lite)\n-   [jsperf test](http://jsperf.com/natural-sort-2/12)\n\n\nLicence\n-------\n\nCopyright (c) 2012, 2014 Lauri Rooden &lt;lauri@rooden.ee&gt;  \n[The MIT License](http://lauri.rooden.ee/mit-license.txt)\n\n\n","readmeFilename":"README.md","homepage":"https://github.com/litejs/natural-compare","_id":"natural-compare@1.2.2","_shasum":"1f96d60e3141cac1b6d05653ce0daeac763af6aa","_from":"https://registry.npmjs.org/natural-compare/-/natural-compare-1.2.2.tgz","_resolved":"https://registry.npmjs.org/natural-compare/-/natural-compare-1.2.2.tgz"}