c# - 字母顺序不比较从左到右?

标签 c# .net string sorting .net-4.0

我认为在 .NET 中字符串是按字母顺序比较的,并且是从左到右比较的。

string[] strings = { "-1", "1", "1Foo", "-1Foo" };
Array.Sort(strings);
Console.WriteLine(string.Join(",", strings));

我希望这个(或两者都以减号开头):

1,1Foo,-1,-1Foo

但结果是:

1,-1,1Foo,-1Foo

这似乎是一种混合,要么忽略减号,要么比较多个字符,即使第一个字符已经不同。

编辑:我现在已经测试了 OrdinalIgnoreCase,我得到了预期的顺序:

Array.Sort(strings, StringComparer.OrdinalIgnoreCase);

但即使我使用 InvariantCultureIgnoreCase 我也会得到意外的顺序。

最佳答案

乔恩双向飞碟救援 here

具体来说:

The .NET Framework uses three distinct ways of sorting: word sort, string sort, and ordinal sort. Word sort performs a culture-sensitive comparison of strings. Certain nonalphanumeric 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. String sort is similar to word sort, except that there are no special cases. Therefore, all nonalphanumeric symbols come before all alphanumeric characters. Ordinal sort compares strings based on the Unicode values of each element of the string.

但是添加 StringComparer.Ordinal 可以让它按照你想要的方式运行:

string[] strings = { "-1", "1", "10", "-10", "a", "ba","-a" };      
Array.Sort(strings,StringComparer.Ordinal );
Console.WriteLine(string.Join(",", strings));
// prints: -1,-10,-a,1,10,a,ba

编辑:
关于序号,引自 MSDN CompareOptions Enumeration

Ordinal Indicates that the string comparison must use successive Unicode UTF-16 encoded values of the string (code unit by code unit comparison), leading to a fast comparison but one that is culture-insensitive. A string starting with a code unit XXXX16 comes before a string starting with YYYY16, if XXXX16 is less than YYYY16. This value cannot be combined with other CompareOptions values and must be used alone.

你似乎也有 String.CompareOrdinal如果你想要 2 个字符串的序数。

这是另一个有趣的注释:

When possible, the application should use string comparison methods that accept a CompareOptions value to specify the kind of comparison expected. As a general rule, user-facing comparisons are best served by the use of linguistic options (using the current culture), while security comparisons should specify Ordinal or OrdinalIgnoreCase.

我猜我们人类在处理字符串时期望序数:)

关于c# - 字母顺序不比较从左到右?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24408416/

相关文章:

c# - 生成随机 boolean 概率

c# - 如何从我的 "jitter"(和 ETA)计算中消除 "estimated time remaining"?

java - 性能:JAVA排列

c# - MVC3 maproute 不显示操作?

python - 这两个Python代码的时间复杂度差异是多少?

c - 显式忽略 NULL 值,但 C 的行为很奇怪

c# - 为什么包含的数据库用户需要 Persist Security Info=True

c# - 使用 WCF 在 .NET 3.5 中定义路由

c# - 创建对象:时出现错误 "an explicit conversion exists (are you missing a cast )"

c# - 事务性 CloudBlob