为什么" -less"经过排序"你好"而不是之前呢? [英] Why does "-less" sort after "hello" instead of before it?

查看:185
本文介绍了为什么" -less"经过排序"你好"而不是之前呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到使用CaseInsensitiveComparer.DefaultInvariant一些非常奇怪的排序行为。单词开始与领先的连字符 - 。最终排序的,如果连字符是不存在,而不是被排序的实际字母前面这与其他标点符号发生了什么

因此​​,考虑{你好,。NET,-less}我最终{.NET,你好,-less},而不是预期的{-less, .NET,你好}。

或者表述为一个测试用例:

  [TestMethod的]
公共无效TestMethod1()
{
    VAR RG =新的String [] {
        ×,Z,Y,-less,达网络, - 多,一个,b的
    };

    的Array.Sort(RG,CaseInsensitiveComparer.DefaultInvariant);

    Assert.AreEqual(
         - 更多的,-less,.NET,A,B,X,Y,Z,
        的string.join(,,RG)
    );
}
 

...这失败是这样的:

  Assert.AreEqual失败。
预计:<  - 更多,-less,.NET,A,B,X,Y,Z取代。
<  - 更多,.NET,A,B,-less,X,Y,Z取代。:实际
 

任何想法是怎么回事?

编辑:

看起来,在默认情况下.NET做花哨的东西整理这导致导致连字符的字符串进行排序到陌生的地方,使合作社和鸡舍排序在一起的时候。因此,如果你希望你的领导连字符的话结束了,并与其他punctutation开始时,你必须告诉它不要不要:

 的Array.Sort(RG,(A,B)=> String.CompareOrdinal(A,B));
 

解决方案
  

比较过程使用CultureInfo.InvariantCulture来确定排序顺序和大小写规则。字符串比较可能会有不同的结果,具体取决于文化。有关区域性特定比较的更多信息,请参见System.Globalization命名空间和编码和本地化。   <一href="http://msdn.microsoft.com/en-us/library/system.collections.caseinsensitivecomparer.defaultinvariant.aspx"相对=nofollow>从这里开始。

有趣的部分:

  

一个字排序进行中,某些非字母数字的Uni code字符可能分配给他们特殊的权重字符串区分区域性的比较。例如,连字符( - )可能具有分配给它的一个非常小的重量,使鸡舍和合作社彼此相邻出现在排序的列表。   从这里开始。

I'm seeing some very strange sorting behaviour using CaseInsensitiveComparer.DefaultInvariant. Words that start with a leading hyphen "-" end up sorted as if the hyphen wasn't there rather than being sorted in front of actual letters which is what happens with other punctuation.

So given { "hello", ".net", "-less"} I end up with {".net", "hello", "-less" } instead of the expected {"-less", ".net", "hello"}.

Or, phrased as a test case:

[TestMethod]
public void TestMethod1()
{
    var rg = new String[] { 
        "x", "z", "y", "-less", ".net", "- more", "a", "b"
    };

    Array.Sort(rg, CaseInsensitiveComparer.DefaultInvariant);

    Assert.AreEqual(
        "- more,-less,.net,a,b,x,y,z", 
        String.Join(",", rg)
    );
}

... which fails like this:

Assert.AreEqual failed. 
Expected:<- more,-less,.net,a,b,x,y,z>. 
Actual:  <- more,.net,a,b,-less,x,y,z>.

Any ideas what's going on?

Edit:

Looks like, by default .NET does fancy things when sorting strings which causes leading hyphens to be sorted into strange places so that co-op and coop sort together. Thus, if you want your leading hyphen words to end up and the begining with the other punctutation you have to tell it not not to:

Array.Sort(rg, (a, b) => String.CompareOrdinal(a, b));

解决方案

Comparison procedures use the CultureInfo.InvariantCulture to determine the sort order and casing rules. String comparisons might have different results depending on the culture. For more information on culture-specific comparisons, see the System.Globalization namespace and Encoding and Localization. From here.

The interesting part:

A word sort performs a culture-sensitive comparison of strings in which certain nonalphanumeric Unicode characters might have special weights assigned to them. For example, the hyphen (-) might have a very small weight assigned to it so that "coop" and "co-op" appear next to each other in a sorted list. From here.

这篇关于为什么&QUOT; -less&QUOT;经过排序&QUOT;你好&QUOT;而不是之前呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆