javascript - JavaScript 中特殊字符的 localCompare 和比较运算符之间的不同结果

标签 javascript sorting comparison

在查看一个与排序相关的问题时,我发现字符串 localeCompare 之间非字母数字字符比较的有趣差异方法和条件运算符(如 >,< )。

您可以通过在不同浏览器中运行以下代码片段来查看它们之间的区别。

function comparison1(param1, param2){
  return param1 > param2;
}

function comparison2(param1, param2){
  return param1.localeCompare(param2);
}
document.getElementById("comparison11").innerHTML = comparison1('A', 'B');
document.getElementById("comparison12").innerHTML = comparison2('A', 'B');
document.getElementById("comparison21").innerHTML = comparison1('@', '_');
document.getElementById("comparison22").innerHTML = comparison2('@', '_');
<div>
  <div style="float: left, width: 100%">
    'A' > 'B'
  </div>
  <div style="float: left, width: 100%" id="comparison11"></div>
  <div style="float: left, width: 100%">
    'A'.localeCompare('B')
  </div>
  <div style="float: left, width: 100%" id="comparison12"></div>
  <div style="float: left, width: 100%">
    '@' > '_'
  </div>
  <div style="float: left, width: 100%" id="comparison21"></div>
  <div style="float: left, width: 100%">
    '@'.localeCompare('_')<br/>
    <i>returns -1 in IE and Edge but 1 in Chrome and Firefox</i>
  </div>
  <div style="float: left, width: 100%" id="comparison22"></div>

</div>
<script>
</script>

如您所见,在使用 localeCompare 时比较“@”和“_”时,响应有所不同。方法和> Chrome 和 Firefox 中的运算符。

我们实现了对具有不同数据类型的多列调用的排序比较方法。所以我们使用了条件运算符,但正如您所见,它在不同的浏览器中为非字母数字字符提供了不同的结果。

这是我的问题!

为什么不同浏览器对特殊字符的响应不同?

什么是实现这个的正确方法? (检查数据类型;如果字符串使用 localeCompare else 条件运算符?)

最佳答案

Why different response for special characters in different browsers?

可能是因为它在 ECMA-402 (internationalization) spec 中说:

Subsets of Unicode: Some operations, such as collation, operate on strings that can include characters from the entire Unicode character set. However, both the Unicode standard and the ECMAScript standard allow implementations to limit their functionality to subsets of the Unicode character set. In addition, locale conventions typically don’t specify the desired behaviour for the entire Unicode character set, but only for those characters that are relevant for the locale. While the Unicode Collation Algorithm combines a default collation order for the entire Unicode character set with the ability to tailor for local conventions, subsets and tailorings still result in differences in behaviour.

最有可能的是,@_ 的顺序在您使用的区域设置(或我的;英国英语)中没有明确定义,因此您得到“行为差异”。

What is correct way to implement this? (Check data type; if string use localeCompare else conditional operator?)

是的。 >< 使用 Unicode 标准中代码点的数字关系,这根本不是处理排序规则的好方法,而 localeCompare 为字符提供了特定于语言环境的排序规则。

明确一点:当您说您“...使用了条件运算符”时,我假设您指的是条件运算符 ( ? : ) 与关系运算符(在本例中为 >< ) ,例如像这样的东西:

return a === b ? 0 : a > b ? 1 : -1;

...或类似的 sort 回调。

但请注意,由于您现在将 localeCompare 用于字符串,并且唯一可以真正有意义地与 >< 进行比较的是数字,因此对于数字有更好的解决方案如果您不知道其中有 NaN :只需减去:

return a - b; // For numbers that aren't NaN

(如果它们中的任何一个可能是 NaN ,您将要处理它——或许使用条件运算符。:-))

关于javascript - JavaScript 中特殊字符的 localCompare 和比较运算符之间的不同结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42482978/

相关文章:

基于票数和 5 星评级的对象评级算法

javascript - 变色按钮

c - C中的慢基数排序

java - 根据数字键的顺序在 map 中排序

arrays - InStr 似乎在此验证子程序中不起作用。有谁知道为什么吗?

python - 有没有一种通用的方法来测试两个对象的属性在 Python 中是否相等?

javascript - 如何在 Javascript 中创建静态字段

javascript - 如何使用 amCharts 舍入用作元素符号的图像?

javascript - 清除浏览器后退按钮上的字段

sorting - 以第一个元素作为主元的快速排序示例