{"name":"string-natural-compare","version":"2.0.0","description":"Compare alphanumeric strings the same way a human would, using a natural order algorithm","author":{"name":"Nathan Woltman","email":"nwoltman@outlook.com"},"license":"MIT","main":"natural-compare.js","files":["natural-compare.js","LICENSE.txt"],"repository":{"type":"git","url":"git+https://github.com/nwoltman/string-natural-compare.git"},"bugs":{"url":"https://github.com/nwoltman/string-natural-compare/issues"},"homepage":"https://github.com/nwoltman/string-natural-compare","devDependencies":{"coveralls":"^2.11.9","grunt":"~1.0.1","grunt-eslint":"^18.0.0","grunt-mocha-istanbul":"^4.0.2","grunt-mocha-test":"^0.12.7","istanbul":"^0.4.3","jit-grunt":"^0.10.0","mocha":"^2.3.2","should":"^8.0.2"},"scripts":{"test":"grunt"},"keywords":["string","natural","compare","comparison","order","natcmp","strnatcmp","sort","natsort","alphanum","alphanumeric"],"gitHead":"3aea25f05620516b1f19e5e448501d690328497a","_id":"string-natural-compare@2.0.0","_shasum":"846b6aa6316a5accf4b475acaa331c6298b1aef5","_from":".","_npmVersion":"3.8.3","_nodeVersion":"5.10.1","_npmUser":{"name":"nwoltman","email":"nwoltman@outlook.com"},"dist":{"shasum":"846b6aa6316a5accf4b475acaa331c6298b1aef5","tarball":"http://registry-npm.ywwl.com/string-natural-compare/download/string-natural-compare-2.0.0.tgz","integrity":"sha512-y6nm3TIbQyw4k6zIcD0LCw9r5fPP/EvHci45Q64u0XQoyJFW9LAXcduu9aiORhVjQcqdAmm4UIDIRXJEJcEH2A==","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIGznQRBK9csWGZqN4G4qKDXR45EEi0GZyPEUWfPRGw9MAiEAxw89+K8RVHYnkXP7GuMDmRO8KYRnI8koshmCfy9kRlY="}],"size":4114,"noattachment":false,"key":"/string-natural-compare/-/string-natural-compare-2.0.0.tgz"},"maintainers":[{"name":"nwoltman","email":"nwoltman@outlook.com"}],"_npmOperationalInternal":{"host":"packages-16-east.internal.npmjs.com","tmp":"tmp/string-natural-compare-2.0.0.tgz_1461207794391_0.4810239088255912"},"directories":{},"readme":"# String Natural Compare\n\n[![NPM Version](https://img.shields.io/npm/v/string-natural-compare.svg)](https://www.npmjs.com/package/string-natural-compare)\n[![Build Status](https://travis-ci.org/nwoltman/string-natural-compare.svg?branch=master)](https://travis-ci.org/nwoltman/string-natural-compare)\n[![Coverage Status](https://coveralls.io/repos/nwoltman/string-natural-compare/badge.svg?branch=master)](https://coveralls.io/r/nwoltman/string-natural-compare?branch=master)\n[![Dependencies Status](https://img.shields.io/david/nwoltman/string-natural-compare)](https://david-dm.org/nwoltman/string-natural-compare)\n\nCompare alphanumeric strings the same way a human would, using a natural order algorithm (originally known as the [alphanum algorithm](http://davekoelle.com/alphanum.html)) where numeric characters are sorted based on their numeric values rather than their ASCII values.\n\n```\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\nThis module exports a function that returns a number indicating whether one string should come before, after, or is the same as another string.\nIt can be used directly with the native [`.sort()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort) array method.\n\n### Fast and Robust\n\nThis module can compare strings containing any size of number and is heavily tested with a custom [benchmark suite](https://github.com/nwoltman/string-natural-compare/tree/master/benchmark) to make sure that it is as fast as possible.\n\n\n## Installation\n\n```sh\nnpm install string-natural-compare --save\n# or\nyarn add string-natural-compare\n```\n\n\n## Usage\n\n#### `naturalCompare(strA, strB[, options])`\n\n+ `strA` (_string_)\n+ `strB` (_string_)\n+ `options` (_object_) - Optional options object with the following options:\n  + `caseInsensitive` (_boolean_) - Set to `true` to compare strings case-insensitively. Default: `false`.\n  + `alphabet` (_string_) - A string of characters that define a custom character ordering. Default: `undefined`.\n\n```js\nconst naturalCompare = require('string-natural-compare');\n\n// Simple, case-sensitive sorting\nconst files = ['z1.doc', 'z10.doc', 'z17.doc', 'z2.doc', 'z23.doc', 'z3.doc'];\nfiles.sort(naturalCompare);\n// -> ['z1.doc', 'z2.doc', 'z3.doc', 'z10.doc', 'z17.doc', 'z23.doc']\n\n\n// Case-insensitive sorting\nconst chars = ['B', 'C', 'a', 'd'];\nconst naturalCompareCI = (a, b) => naturalCompare(a, b, {caseInsensitive: true});\nchars.sort(naturalCompareCI);\n// -> ['a', 'B', 'C', 'd']\n\n// Note:\n['a', 'A'].sort(naturalCompareCI); // -> ['a', 'A']\n['A', 'a'].sort(naturalCompareCI); // -> ['A', 'a']\n\n\n// Compare strings containing large numbers\nnaturalCompare(\n  '1165874568735487968325787328996865',\n  '265812277985321589735871687040841'\n);\n// -> 1\n// (Other inputs with the same ordering as this example may yield a different number > 0)\n\n\n// Sorting an array of objects\nconst hotelRooms = [\n  {street: '350 5th Ave', room: 'A-1021'},\n  {street: '350 5th Ave', room: 'A-21046-b'}\n];\n// Sort by street (case-insensitive), then by room (case-sensitive)\nhotelRooms.sort((a, b) => (\n  naturalCompare(a.street, b.street, {caseInsensitive: true}) ||\n  naturalCompare(a.room, b.room)\n));\n\n\n// When text transformation is needed or when doing a case-insensitive sort on a\n// large array of objects, it is best for performance to pre-compute the\n// transformed text and store it on the object. This way, the text will not need\n// to be transformed for every comparison while sorting.\nconst cars = [\n  {make: 'Audi', model: 'R8'},\n  {make: 'Porsche', model: '911 Turbo S'}\n];\n// Sort by make, then by model (both case-insensitive)\nfor (const car of cars) {\n  car.sortKey = (car.make + ' ' + car.model).toLowerCase();\n}\ncars.sort((a, b) => naturalCompare(a.sortKey, b.sortKey));\n\n\n// Using a custom alphabet (Russian alphabet)\nconst russianOpts = {\n  alphabet: 'АБВГДЕЁЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯабвгдеёжзийклмнопрстуфхцчшщъыьэюя',\n};\n['Ё', 'А', 'б', 'Б'].sort((a, b) => naturalCompare(a, b, russianOpts));\n// -> ['А', 'Б', 'Ё', 'б']\n```\n\n**Note:** Putting numbers in the custom alphabet can cause undefined behaviour.\n","publish_time":1461207795747,"_cnpm_publish_time":1461207795747,"dist-tags":{"latest":"3.0.1"}}