JavaScript 有 strcmp() 吗?

发布于 2024-07-30 00:46:58 字数 167 浏览 4 评论 0原文

有人可以帮我验证一下吗? JavaScript 没有 strcmp() 版本,因此您必须写出如下内容:

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

夜深人未静 2024-08-06 00:46:59

正如你所指出的,Javascript 没有它。

快速搜索得出:

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

来自 http://kevin.vanzonneveld.net/techblog/article/ javascript_equivalent_for_phps_strcmp/

当然,如果需要,您可以只添加 localeCompare:

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

并在任何地方使用 str1.localeCompare(str2),而不必担心本地浏览器是否已附带它。 唯一的问题是您必须添加对 区域设置选项(如果您关心的话)。

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.

泅渡 2024-08-06 00:46:59

localeCompare() 很慢,所以如果你不关心“正确”的顺序对于非英文字符串,请尝试原始方法或看起来更简洁的方法:

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

这比我的机器上的 localeCompare() 快一个数量级。

+ 确保答案始终是数字而不是布尔值。

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.

疏忽 2024-08-06 00:46:59
var strcmp = new Intl.Collator(undefined, {numeric:true, sensitivity:'base'}).compare;

用法:strcmp(string1, string2)

结果:1 表示 string1 更大,0 表示相等,-1意味着 string2 更大。

这比 String.prototype.localeCompare 具有更高的性能,

而且 numeric:true 使其可以进行逻辑数字比较

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

﹎☆浅夏丿初晴 2024-08-06 00:46:59

来自这篇如何在 JavaScript 中检查两个字符串是否相等文章:

  1. 一般情况,如果您的字符串仅包含 ASCII 字符,则可以使用 === 运算符来检查它们是否相等。
  2. 但是,当您的字符串包含包含组合字符的字符(例如 e + ◌́ = é)时,您首先要对它们进行规范化,然后再比较相等性,如下所示 -
    s1.normalize() === s2.normalize()

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()
风尘浪孓 2024-08-06 00:46:59

所以我陷入了这个兔子洞并编写了一些测试来建立直觉,结果很奇怪。
tldr 看起来像 localeCompare 执行小写
相等运算符则不然。 这会导致“ff”>=“ZZ”,但区域设置比较返回-1,因为“ff”<=“zz”

为了获得最佳结果,请在浏览器控制台中查看代码日志ctrl + shift + i

第二个片段,它会隐藏手动测试所以你会看到一些随机的。

主页这对某人有帮助

function stringBench(n, bench, min = 10, len = 10, logDif = false) {
  function makeid(length) {
    var result = '';
    var characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
    var charactersLength = characters.length;
    for (var i = 0; i < length; i++) {
      result += characters.charAt(Math.floor(Math.random() *
        charactersLength));
    }
    return result;

  }

  var a = [];
  var b = [];
  var pool = {};
  let rle = [];
  let rlc = [];

  let now;
  for (let i = 0; i < n; i++) {
    pool[i] = (makeid(min + Math.floor(Math.random() *
      len))); //10-20ish

  }
  for (let i = 0; i < bench; i++) {
    a[i] = (Math.floor(Math.random() * n));
    b[i] = (Math.floor(Math.random() * n));
  }

  console.log("now testin le vs lc on a pool of", n, " with this many samples ", bench);
  now = Date.now();
  for (let i = 0; i < bench; i++) {
    rlc[i] = pool[a[i]].localeCompare(pool[b[i]]);
  }
  let lcDelta = Date.now() - now;
  console.log("Performed ", bench, "lc localeCompare in ", lcDelta);


  now = Date.now();
  for (let i = 0; i < bench; i++) {
    rle[i] = pool[a[i]] <= pool[b[i]];
  }
  let leDelta = Date.now() - now;
  console.log("Performed ", bench, "le (<=) compares in ", leDelta)

  for (let i = 0; i < n; i++) {
    pool[i] = (makeid(min + Math.floor(Math.random() *
      len))); //10-20ish

  }
  for (let i = 0; i < bench; i++) {
    a[i] = (Math.floor(Math.random() * n));
    b[i] = (Math.floor(Math.random() * n));
  }


  now = Date.now();
  for (let i = 0; i < bench; i++) {
    rle[i] = pool[a[i]] <= pool[b[i]];
  }
  let leDelta2 = Date.now() - now;
  console.log("Performed ", bench, "le (<=) compares in ", leDelta2)

  now = Date.now();
  for (let i = 0; i < bench; i++) {
    rlc[i] = pool[a[i]].localeCompare(pool[b[i]]);
  }
  let lcDelta2 = Date.now() - now;
  console.log("Performed ", bench, "lc localeCompare in ", lcDelta2);

  function testCmp(a, b, log = true) {
    let le = a <= b;
    let ge = a >= b;
    let lc = a.localeCompare(b);
    let l = a < b;
    let g = a > b;
    if (le && ge) console.assert(lc == 0, 'le && ge -> == -> lc == 0,')
    if (le) console.assert(lc <= 0, 'le-> lc <= 0')
    if (ge) console.assert(lc >= 0, 'ge-> lc >= 0')
    if (l) console.assert(lc < 0, 'l=>lc < 0')
    if (g) console.assert(lc > 0, 'g-> lc > 0')
    if (!log) return;
    console.log(`Compare:  ${a} le ${b} `, a <= b);
    console.log(`Compare:  ${a} ge ${b}`, a >= b);
    console.log(`Compare: ${a} lc ${b}`, a.localeCompare(b));
  }

  let c = 0
  for (let i = 0; i < bench; i++) {
    if (rle[i] != rlc[i] <= 0) {
      c++;
      testCmp(pool[a[i]], pool[b[i]], true);
      console.warn(pool[a[i]], ' le != lc <= 0 ', pool[b[i]]);

    }


    // rlc[i] = pool[a[i]].localeCompare(pool[b[i]]);
  }
  console.warn(' le != lc  out of bench, num diffs: ', c);


  testCmp('ff', 'ff')
  testCmp('ff', 'fa')
  testCmp('ff', 'fz')
  testCmp('ff', 'fff')
  testCmp('ff', 'ffa')
  testCmp('ff', 'ffz')
  testCmp('ff', 'a')
  testCmp('ff', 'z')
  testCmp('ff', 'f')
  testCmp('ff', 'zff')
  testCmp('ff', 'aff')
  testCmp('ff', 'ZZ')
  testCmp('ff', 'AA')
  testCmp('FF', 'ZZ')
  testCmp('FF', 'ff')
  testCmp('FF', 'AA')
  testCmp('ff', 'ZZZ')

  console.log("Dif le - lc = ", leDelta2 - lcDelta2);

  console.log("avg le ms/Mops = ", (leDelta + leDelta2) / (bench / 1000000));
  console.log("avg lc ms/Mops = ", (lcDelta + lcDelta2) / (bench / 1000000));


  console.log("Dif  - lc = ", leDelta2 - lcDelta2);

};
stringBench(1000, 5000, 1, 3, true);
// stringBench(1000000, 1000000);//nothing is equire
// stringBench(1000, 100000000);
// stringBench(1000000, 100000000, 3, 5);
// stringBench(1000000, 100000000, 15, 20);

function stringBench(n, bench, min = 10, len = 10, logDif = false) {
  function makeid(length) {
    var result = '';
    var characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
    var charactersLength = characters.length;
    for (var i = 0; i < length; i++) {
      result += characters.charAt(Math.floor(Math.random() *
        charactersLength));
    }
    return result;


  }

  var a = [];
  var b = [];
  var pool = {};
  let rle = [];
  let rlc = [];

  let now;
  for (let i = 0; i < n; i++) {
    pool[i] = (makeid(min + Math.floor(Math.random() *
      len))); //10-20ish

  }
  for (let i = 0; i < bench; i++) {
    a[i] = (Math.floor(Math.random() * n));
    b[i] = (Math.floor(Math.random() * n));
  }

  console.log("now testin le vs lc on a pool of", n, " with this many samples ", bench);
  now = Date.now();
  for (let i = 0; i < bench; i++) {
    rlc[i] = pool[a[i]].localeCompare(pool[b[i]]);
  }
  let lcDelta = Date.now() - now;
  console.log("Performed ", bench, "lc localeCompare in ", lcDelta);


  now = Date.now();
  for (let i = 0; i < bench; i++) {
    rle[i] = pool[a[i]] <= pool[b[i]];
  }
  let leDelta = Date.now() - now;
  console.log("Performed ", bench, "le (<=) compares in ", leDelta)

  for (let i = 0; i < n; i++) {
    pool[i] = (makeid(min + Math.floor(Math.random() *
      len))); //10-20ish

  }
  for (let i = 0; i < bench; i++) {
    a[i] = (Math.floor(Math.random() * n));
    b[i] = (Math.floor(Math.random() * n));
  }


  now = Date.now();
  for (let i = 0; i < bench; i++) {
    rle[i] = pool[a[i]] <= pool[b[i]];
  }
  let leDelta2 = Date.now() - now;
  console.log("Performed ", bench, "le (<=) compares in ", leDelta2)

  now = Date.now();
  for (let i = 0; i < bench; i++) {
    rlc[i] = pool[a[i]].localeCompare(pool[b[i]]);
  }
  let lcDelta2 = Date.now() - now;
  console.log("Performed ", bench, "lc localeCompare in ", lcDelta2);

  function testCmp(a, b, log = true) {
    let le = a <= b;
    let ge = a >= b;
    let lc = a.localeCompare(b);
    let l = a < b;
    let g = a > b;
    if (le && ge) console.assert(lc == 0, 'le && ge -> == -> lc == 0,')
    if (le) console.assert(lc <= 0, 'le-> lc <= 0')
    if (ge) console.assert(lc >= 0, 'ge-> lc >= 0')
    if (l) console.assert(lc < 0, 'l=>lc < 0')
    if (g) console.assert(lc > 0, 'g-> lc > 0')
    if (!log) return;
    console.log(`Compare:  ${a} le ${b} `, a <= b);
    console.log(`Compare:  ${a} ge ${b}`, a >= b);
    console.log(`Compare: ${a} lc ${b}`, a.localeCompare(b));
  }

  let c = 0
  for (let i = 0; i < bench; i++) {
    if (rle[i] != rlc[i] <= 0) {
      c++;
      testCmp(pool[a[i]], pool[b[i]], true);
      console.warn(pool[a[i]], ' le != lc <= 0 ', pool[b[i]]);

    }


    // rlc[i] = pool[a[i]].localeCompare(pool[b[i]]);
  }
  console.warn(' le != lc  out of bench, num diffs: ', c);



  testCmp('ff', 'fa')
  testCmp('ff', 'fz')
  testCmp('ff', 'ZZ')



  console.log("Dif le - lc = ", leDelta2 - lcDelta2);

  console.log("avg le ms/Mops = ", (leDelta + leDelta2) / (bench / 1000000));
  console.log("avg lc ms/Mops = ", (lcDelta + lcDelta2) / (bench / 1000000));







  console.log("Dif  - lc = ", leDelta2 - lcDelta2);


  // for (let i = 0; i < bench; i++) {
  //     rlc[i] != rle[i]
  //     pool[a[i]].localeCompare(pool[b[i]]);
  // }
  //
  // console.log(makeid(5));
};
stringBench(1000, 5000, 1, 3, true);
// stringBench(1000000, 1000000);//nothing is equire
// stringBench(1000, 100000000);
// stringBench(1000000, 100000000, 3, 5);
// stringBench(1000000, 100000000, 15, 20);

So I fell into this rabbit hole and wrote some tests to build an intuition, the result's are weird.
tldr it looks like localeCompare Performs a to lowercase that
the equality operators do not. This causes "ff" to be >= "ZZ" but locale Compare returns -1 becuse "ff" <= 'zz'

For best results view logs of code in browser console ctrl + shift + i

second snip it hides hand tests so you see some random ones.

Home this helps someone

function stringBench(n, bench, min = 10, len = 10, logDif = false) {
  function makeid(length) {
    var result = '';
    var characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
    var charactersLength = characters.length;
    for (var i = 0; i < length; i++) {
      result += characters.charAt(Math.floor(Math.random() *
        charactersLength));
    }
    return result;

  }

  var a = [];
  var b = [];
  var pool = {};
  let rle = [];
  let rlc = [];

  let now;
  for (let i = 0; i < n; i++) {
    pool[i] = (makeid(min + Math.floor(Math.random() *
      len))); //10-20ish

  }
  for (let i = 0; i < bench; i++) {
    a[i] = (Math.floor(Math.random() * n));
    b[i] = (Math.floor(Math.random() * n));
  }

  console.log("now testin le vs lc on a pool of", n, " with this many samples ", bench);
  now = Date.now();
  for (let i = 0; i < bench; i++) {
    rlc[i] = pool[a[i]].localeCompare(pool[b[i]]);
  }
  let lcDelta = Date.now() - now;
  console.log("Performed ", bench, "lc localeCompare in ", lcDelta);


  now = Date.now();
  for (let i = 0; i < bench; i++) {
    rle[i] = pool[a[i]] <= pool[b[i]];
  }
  let leDelta = Date.now() - now;
  console.log("Performed ", bench, "le (<=) compares in ", leDelta)

  for (let i = 0; i < n; i++) {
    pool[i] = (makeid(min + Math.floor(Math.random() *
      len))); //10-20ish

  }
  for (let i = 0; i < bench; i++) {
    a[i] = (Math.floor(Math.random() * n));
    b[i] = (Math.floor(Math.random() * n));
  }


  now = Date.now();
  for (let i = 0; i < bench; i++) {
    rle[i] = pool[a[i]] <= pool[b[i]];
  }
  let leDelta2 = Date.now() - now;
  console.log("Performed ", bench, "le (<=) compares in ", leDelta2)

  now = Date.now();
  for (let i = 0; i < bench; i++) {
    rlc[i] = pool[a[i]].localeCompare(pool[b[i]]);
  }
  let lcDelta2 = Date.now() - now;
  console.log("Performed ", bench, "lc localeCompare in ", lcDelta2);

  function testCmp(a, b, log = true) {
    let le = a <= b;
    let ge = a >= b;
    let lc = a.localeCompare(b);
    let l = a < b;
    let g = a > b;
    if (le && ge) console.assert(lc == 0, 'le && ge -> == -> lc == 0,')
    if (le) console.assert(lc <= 0, 'le-> lc <= 0')
    if (ge) console.assert(lc >= 0, 'ge-> lc >= 0')
    if (l) console.assert(lc < 0, 'l=>lc < 0')
    if (g) console.assert(lc > 0, 'g-> lc > 0')
    if (!log) return;
    console.log(`Compare:  ${a} le ${b} `, a <= b);
    console.log(`Compare:  ${a} ge ${b}`, a >= b);
    console.log(`Compare: ${a} lc ${b}`, a.localeCompare(b));
  }

  let c = 0
  for (let i = 0; i < bench; i++) {
    if (rle[i] != rlc[i] <= 0) {
      c++;
      testCmp(pool[a[i]], pool[b[i]], true);
      console.warn(pool[a[i]], ' le != lc <= 0 ', pool[b[i]]);

    }


    // rlc[i] = pool[a[i]].localeCompare(pool[b[i]]);
  }
  console.warn(' le != lc  out of bench, num diffs: ', c);


  testCmp('ff', 'ff')
  testCmp('ff', 'fa')
  testCmp('ff', 'fz')
  testCmp('ff', 'fff')
  testCmp('ff', 'ffa')
  testCmp('ff', 'ffz')
  testCmp('ff', 'a')
  testCmp('ff', 'z')
  testCmp('ff', 'f')
  testCmp('ff', 'zff')
  testCmp('ff', 'aff')
  testCmp('ff', 'ZZ')
  testCmp('ff', 'AA')
  testCmp('FF', 'ZZ')
  testCmp('FF', 'ff')
  testCmp('FF', 'AA')
  testCmp('ff', 'ZZZ')

  console.log("Dif le - lc = ", leDelta2 - lcDelta2);

  console.log("avg le ms/Mops = ", (leDelta + leDelta2) / (bench / 1000000));
  console.log("avg lc ms/Mops = ", (lcDelta + lcDelta2) / (bench / 1000000));


  console.log("Dif  - lc = ", leDelta2 - lcDelta2);

};
stringBench(1000, 5000, 1, 3, true);
// stringBench(1000000, 1000000);//nothing is equire
// stringBench(1000, 100000000);
// stringBench(1000000, 100000000, 3, 5);
// stringBench(1000000, 100000000, 15, 20);

function stringBench(n, bench, min = 10, len = 10, logDif = false) {
  function makeid(length) {
    var result = '';
    var characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
    var charactersLength = characters.length;
    for (var i = 0; i < length; i++) {
      result += characters.charAt(Math.floor(Math.random() *
        charactersLength));
    }
    return result;


  }

  var a = [];
  var b = [];
  var pool = {};
  let rle = [];
  let rlc = [];

  let now;
  for (let i = 0; i < n; i++) {
    pool[i] = (makeid(min + Math.floor(Math.random() *
      len))); //10-20ish

  }
  for (let i = 0; i < bench; i++) {
    a[i] = (Math.floor(Math.random() * n));
    b[i] = (Math.floor(Math.random() * n));
  }

  console.log("now testin le vs lc on a pool of", n, " with this many samples ", bench);
  now = Date.now();
  for (let i = 0; i < bench; i++) {
    rlc[i] = pool[a[i]].localeCompare(pool[b[i]]);
  }
  let lcDelta = Date.now() - now;
  console.log("Performed ", bench, "lc localeCompare in ", lcDelta);


  now = Date.now();
  for (let i = 0; i < bench; i++) {
    rle[i] = pool[a[i]] <= pool[b[i]];
  }
  let leDelta = Date.now() - now;
  console.log("Performed ", bench, "le (<=) compares in ", leDelta)

  for (let i = 0; i < n; i++) {
    pool[i] = (makeid(min + Math.floor(Math.random() *
      len))); //10-20ish

  }
  for (let i = 0; i < bench; i++) {
    a[i] = (Math.floor(Math.random() * n));
    b[i] = (Math.floor(Math.random() * n));
  }


  now = Date.now();
  for (let i = 0; i < bench; i++) {
    rle[i] = pool[a[i]] <= pool[b[i]];
  }
  let leDelta2 = Date.now() - now;
  console.log("Performed ", bench, "le (<=) compares in ", leDelta2)

  now = Date.now();
  for (let i = 0; i < bench; i++) {
    rlc[i] = pool[a[i]].localeCompare(pool[b[i]]);
  }
  let lcDelta2 = Date.now() - now;
  console.log("Performed ", bench, "lc localeCompare in ", lcDelta2);

  function testCmp(a, b, log = true) {
    let le = a <= b;
    let ge = a >= b;
    let lc = a.localeCompare(b);
    let l = a < b;
    let g = a > b;
    if (le && ge) console.assert(lc == 0, 'le && ge -> == -> lc == 0,')
    if (le) console.assert(lc <= 0, 'le-> lc <= 0')
    if (ge) console.assert(lc >= 0, 'ge-> lc >= 0')
    if (l) console.assert(lc < 0, 'l=>lc < 0')
    if (g) console.assert(lc > 0, 'g-> lc > 0')
    if (!log) return;
    console.log(`Compare:  ${a} le ${b} `, a <= b);
    console.log(`Compare:  ${a} ge ${b}`, a >= b);
    console.log(`Compare: ${a} lc ${b}`, a.localeCompare(b));
  }

  let c = 0
  for (let i = 0; i < bench; i++) {
    if (rle[i] != rlc[i] <= 0) {
      c++;
      testCmp(pool[a[i]], pool[b[i]], true);
      console.warn(pool[a[i]], ' le != lc <= 0 ', pool[b[i]]);

    }


    // rlc[i] = pool[a[i]].localeCompare(pool[b[i]]);
  }
  console.warn(' le != lc  out of bench, num diffs: ', c);



  testCmp('ff', 'fa')
  testCmp('ff', 'fz')
  testCmp('ff', 'ZZ')



  console.log("Dif le - lc = ", leDelta2 - lcDelta2);

  console.log("avg le ms/Mops = ", (leDelta + leDelta2) / (bench / 1000000));
  console.log("avg lc ms/Mops = ", (lcDelta + lcDelta2) / (bench / 1000000));







  console.log("Dif  - lc = ", leDelta2 - lcDelta2);


  // for (let i = 0; i < bench; i++) {
  //     rlc[i] != rle[i]
  //     pool[a[i]].localeCompare(pool[b[i]]);
  // }
  //
  // console.log(makeid(5));
};
stringBench(1000, 5000, 1, 3, true);
// stringBench(1000000, 1000000);//nothing is equire
// stringBench(1000, 100000000);
// stringBench(1000000, 100000000, 3, 5);
// stringBench(1000000, 100000000, 15, 20);

年华零落成诗 2024-08-06 00:46:59

在我的测试中,这比对同一组随机选择的单词使用一对三元语句快大约 10%。

function strcmp( a, b ) {
    for( let i=0 ; i<Math.min( a.length, b.length ) ; i++ ) {
        const n = a.charCodeAt(i) - b.charCodeAt(i);
        if( n ) return  n && ( ( n>>31 ) || 1 );
    }
    const n = a.length - b.length;
    return  n && ( ( n>>31 ) || 1 );
}

In my tests, this is about 10% faster than using a pair of ternary statements on the same set of randomly selected words.

function strcmp( a, b ) {
    for( let i=0 ; i<Math.min( a.length, b.length ) ; i++ ) {
        const n = a.charCodeAt(i) - b.charCodeAt(i);
        if( n ) return  n && ( ( n>>31 ) || 1 );
    }
    const n = a.length - b.length;
    return  n && ( ( n>>31 ) || 1 );
}
彡翼 2024-08-06 00:46:59

怎么样:

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

然后,将 s1 与 2 进行比较:

s1.strcmp(s2)

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)
始终不够爱げ你 2024-08-06 00:46:58

关于什么

str1.localeCompare(str2)

What about

str1.localeCompare(str2)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文