Is there a JavaScript strcmp()?

JavascriptStringStrcmp

Javascript Problem Overview


Can anyone verify this for me? JavaScript does not have a version of strcmp(), so you have to write out something like:

 ( str1 < str2 ) ? 
            -1 : 
             ( str1 > str2 ? 1 : 0 );

Javascript Solutions


Solution 1 - Javascript

What about

str1.localeCompare(str2)

Solution 2 - Javascript

Javascript doesn't have it, as you point out.

A quick search came up with:

function strcmp ( str1, str2 ) {
    // 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
    // *     example 1: strcmp( 'waldo', 'owald' );
    // *     returns 1: 1
    // *     example 2: strcmp( 'owald', 'waldo' );
    // *     returns 2: -1
 
    return ( ( str1 == str2 ) ? 0 : ( ( str1 > str2 ) ? 1 : -1 ) );
}

from http://kevin.vanzonneveld.net/techblog/article/javascript_equivalent_for_phps_strcmp/

Of course, you could just add localeCompare if needed:

if (typeof(String.prototype.localeCompare) === 'undefined') {
    String.prototype.localeCompare = function(str, locale, options) {
        return ((this == str) ? 0 : ((this > str) ? 1 : -1));
    };
}

And use str1.localeCompare(str2) everywhere, without having to worry wether the local browser has shipped with it. The only problem is that you would have to add support for locales and options if you care about that.

Solution 3 - Javascript

localeCompare() is slow, so if you don't care about the "correct" ordering of non-English-character strings, try your original method or the cleaner-looking:

str1 < str2 ? -1 : +(str1 > str2)

This is an order of magnitude faster than localeCompare() on my machine.

The + ensures that the answer is always numeric rather than boolean.

Solution 4 - Javascript

var strcmp = new Intl.Collator(undefined, {numeric:true, sensitivity:'base'}).compare;

Usage: strcmp(string1, string2)

Result: 1 means string1 is bigger, 0 means equal, -1 means string2 is bigger.

This has higher performance than String.prototype.localeCompare

Also, numeric:true makes it do logical number comparison

Solution 5 - Javascript

from this How to Check if Two Strings are Equal in JavaScript article:

  1. Generally, if your strings contain only ASCII characters, you use the === operator to check if they are equal.

  2. But when your strings contain characters that include combining characters(eg. e + ◌́ = é), you normalize them first before comparing for equality as follows- s1.normalize() === s2.normalize()

Solution 6 - Javascript

How about:

String.prototype.strcmp = function(s) {
	if (this < s) return -1;
	if (this > s) return 1;
	return 0;
}

Then, to compare s1 with 2:

s1.strcmp(s2)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionjonathanView Question on Stackoverflow
Solution 1 - JavascriptnewacctView Answer on Stackoverflow
Solution 2 - JavascriptEsteban KüberView Answer on Stackoverflow
Solution 3 - Javascript1''View Answer on Stackoverflow
Solution 4 - JavascriptAnonymousView Answer on Stackoverflow
Solution 5 - JavascriptRunay DhaygudeView Answer on Stackoverflow
Solution 6 - JavascriptPanos PapadopoulosView Answer on Stackoverflow