Use PHP functions in JavaScript

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 ) );
}
external links: original PHP docs | raw js source

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.

Comments

Add Comment
Use:
[CODE]
your_stuff('here');
[/CODE]
for proper code formatting
By submitting code here you are allowing us to use it in php.js hence dual licensing it under the MIT and GPL licenses

Gravatar
Kevin van Zonneveld
4 Aug '09 Permalink

q  @ coderjoe & Brett Zamir: awesome : )

Gravatar
Brett Zamir
29 Jul '09 Permalink

q  @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()).

Gravatar
coderjoe
24 Jul '09 Permalink

q  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);
}


Gravatar
coderjoe
24 Jul '09 Permalink

q  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);
}


Gravatar
narendra
8 Apr '09 Permalink

q  just fun

Gravatar
Kevin van Zonneveld
1 Feb '09 Permalink

q  @ Brett Zamir: sweet!

Gravatar
Brett Zamir
28 Jan '09 Permalink

q  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]

Gravatar
Kevin van Zonneveld
18 Jun '08 Permalink

q  @ gorthaur: I must admit I personally never use this function in PHP. Thanks for improving php.js.

Gravatar
gorthaur
18 Jun '08 Permalink

q  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]

Gravatar
Kevin van Zonneveld
8 May '08 Permalink

q  @ Steve Hilder: I must say I'm not very familiar with this specific function, but I did some reading up on it, and I think I agree that in it's current form it makes no sense. I've updated it so calculate every character in both strings, this is better right.

Gravatar
Steve Hilder
8 May '08 Permalink

q  Err... this doesn't work at all; it only evaluates the first character.

[CODE="Javascript"]strcmp('test', 'tomato') = 0 /* incorrect */[/CODE]


Contribute a New function