gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 23:22:42 25 4
gpt4 key购买 nike

我认为在 .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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com