JavaScript strnatcmp
Returns the result of string comparison using 'natural' algorithm
1 2 3 4 56 7 8 9 1011 12 13 14 1516 17 18 19 2021 22 23 24 2526 27 28 29 3031 32 33 34 3536 37 38 39 4041 42 43 44 4546 47 48 49 5051 52 53 54 5556 57 58 59 6061 62 63 64 6566 67 68 69 7071 72 73 74 7576 77 78 79 8081 82 83 84 8586 87 88 89 9091 92 93 94 9596 97 98 99 100101 102 103 104 105106 107 108 109 110111 112 113 114 115116 117 118 119 120121 122 123 124 | function strnatcmp ( f_string1, f_string2, f_version ) { // Returns the result of string comparison using 'natural' algorithm // // version: 1008.1718 // discuss at: http://phpjs.org/functions/strnatcmp // + original by: Martijn Wieringa // + namespaced by: Michael White (http://getsprink.com) // + tweaked by: Jack // + bugfixed by: Onno Marsman // - depends on: strcmp // % note: Added f_version argument against code guidelines, because it's so neat // * example 1: strnatcmp('Price 12.9', 'Price 12.15'); // * returns 1: 1 // * example 2: strnatcmp('Price 12.09', 'Price 12.15'); // * returns 2: -1 // * example 3: strnatcmp('Price 12.90', 'Price 12.15'); // * returns 3: 1 // * example 4: strnatcmp('Version 12.9', 'Version 12.15', true); // * returns 4: -6 // * example 5: strnatcmp('Version 12.15', 'Version 12.9', true); // * returns 5: 6 var i = 0; if (f_version == undefined) { f_version = false; } var __strnatcmp_split = function ( f_string ) { var result = []; var buffer = ''; var chr = ''; var i = 0, f_stringl = 0; var text = true; f_stringl = f_string.length; for (i = 0; i < f_stringl; i++) { chr = f_string.substring(i, i + 1); if (chr.match(/\d/)) { if (text) { if (buffer.length > 0){ result[result.length] = buffer; buffer = ''; } text = false; } buffer += chr; } else if ((text == false) && (chr == '.') && (i < (f_string.length - 1)) && (f_string.substring(i + 1, i + 2).match(/\d/))) { result[result.length] = buffer; buffer = ''; } else { if (text == false) { if (buffer.length > 0) { result[result.length] = parseInt(buffer, 10); buffer = ''; } text = true; } buffer += chr; } } if (buffer.length > 0) { if (text) { result[result.length] = buffer; } else { result[result.length] = parseInt(buffer, 10); } } return result; }; var array1 = __strnatcmp_split(f_string1+''); var array2 = __strnatcmp_split(f_string2+''); var len = array1.length; var text = true; var result = -1; var r = 0; if (len > array2.length) { len = array2.length; result = 1; } for (i = 0; i < len; i++) { if (isNaN(array1[i])) { if (isNaN(array2[i])) { text = true; if ((r = this.strcmp(array1[i], array2[i])) != 0) { return r; } } else if (text){ return 1; } else { return -1; } } else if (isNaN(array2[i])) { if (text) { return -1; } else{ return 1; } } else { if (text || f_version){ if ((r = (array1[i] - array2[i])) != 0) { return r; } } else { if ((r = this.strcmp(array1[i].toString(), array2[i].toString())) != 0) { return r; } } text = false; } } return result; } |
Examples
» Example 1
Running
1 | strnatcmp('Price 12.9', 'Price 12.15'); |
Should return
1 | 1 |
» Example 2
Running
1 | strnatcmp('Price 12.09', 'Price 12.15'); |
Should return
1 | -1 |
Dependencies
In order to use this function, you also need:
Open syntax issues
php.js uses JsLint to help us keep our code consistent and prevent some common bugs.
Eventually we want all code to pass or at least take into consideration most fixes suggested by JsLint, following this JsLint configuration we’ve decided on.
Authors
Thanks to the following developers, you get to have strnatcmp goodness in JavaScript.
The args 'f_version' is missing in the function definition.
I've added this tag to fix the following problem:
When you're comparing numbers in a string.. like:
"Price 12.9"
"Price 12.15"
You want: "Price 12.9" > "Price 12.15".
Yet when you use numbers to indicate version numbers.. Like
"Version 12.9"
"Version 12.15"
You want: "Price 12.9" < "Price 12.15".


Kevin van Zonneveld
1 Mar '08