JavaScript strcmp
Binary safe string comparison
1 2 3 4 56 7 8 9 1011 12 13 14 15 | function strcmp ( str1, str2 ) { // Binary safe string comparison // // version: 1008.1718 // discuss at: http://phpjs.org/functions/strcmp // + original by: Waldo Malqui Silva // + input by: Steve Hilder // + improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net) // + revised by: gorthaur // * example 1: strcmp( 'waldo', 'owald' ); // * returns 1: 1 // * example 2: strcmp( 'owald', 'waldo' ); // * returns 2: -1 return ( ( str1 == str2 ) ? 0 : ( ( str1 > str2 ) ? 1 : -1 ) ); } |
Examples
» Example 1
Running
1 | strcmp( 'waldo', 'owald' ); |
Should return
1 | 1 |
» Example 2
Running
1 | strcmp( 'owald', 'waldo' ); |
Should return
1 | -1 |
Dependencies
No dependencies, you can use this function standalone.
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 strcmp goodness in JavaScript.
@coderjoe: Up until you mentioned it, we were using strcoll() to do this, since that is PHP's locale-specific version; however, in SVN, I just changed the behavior of strcoll() to avoid using this built-in but non-transparent JS locale-aware sort function in favor of letting strcoll()'s behavior be configurable through setlocale() (which I also just modified--LC_COLLATE to be specific). This will let people set the locale to whatever locale they wish (though we currently only have an English collating function implemented in setlocale()).
Couldn't this use ECMA-262's String.prototype.localeCompare function?
function strcmp ( str1, str2 ) {
// Binary safe string comparison
// using ECMA-262 section 15.5.4.9
// String.prototype.localeCompare
return str1.localeCompare(str2);
}
Couldn't this use ECMA-262's String.prototype.localeCompare function?
function strcmp ( str1, str2 ) {
// Binary safe string comparison
// using ECMA-262 section 15.5.4.9
// String.prototype.localeCompare
return str1.localeCompare(str2);
}
Here's a related one...
[CODE="Javascript"]
function strncmp ( str1, str2, lgth ) {
// http://kevin.vanzonneveld.net
// + original by: Waldo Malqui Silva
// + input by: Steve Hilder
// + improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
// + revised by: gorthaur
s1 = str1.substr(0, lgth);
s2 = str2.substr(0, lgth);
return ( ( s1 == s2 ) ? 0 : ( ( s1 > s2 ) ? 1 : -1 ) );
}
alert(strncmp('aaa', 'aab', 2)); // 0
alert(strncmp('aaa', 'aab', 3)); // -1[/CODE]
You gotta be kidding! This code is unbelievably silly and displays gross lack of understanding string comparison. Try these test cases
[CODE="Javascript"]
strcmp( 'waldo', 'owald' );
strcmp( 'owald', 'waldo' );
[/CODE]
which should return +1 and -1.
This should work:
[CODE="Javascript"]
function strcmp ( str1, str2 ) {
return ( ( str1 == str2 ) ? 0 : ( ( str1 > str2 ) ? 1 : -1 ) );
}
[/CODE]


Kevin van Zonneveld
4 Aug '09