在这种情况下,StringComparer.CurrentCulture 是正确的选择吗?
我有一个 UTF-8 字符串列表,我想使用 Enumerable.OrderBy。字符串可以包含任意数量的字符集 - 例如英语、德语和日语,甚至它们的混合。
例如,这里是一个示例输入列表:
["東京","North 東京", "München", "New York", "Chicago", "大阪市"]
我很困惑是否使用 StringComparer.CurrentCulture 是传递给 OrderBy()
的正确字符串比较参数。如果应用程序的当前区域性是 en-US
,但我仍想“正确”对 UTF-8 数据进行排序,而不仅仅是 en-US
排序规则,该怎么办?
我的困惑可能源于我对 Oracle 中 NLSSORT 函数的理解与 .NET 字符串比较和排序语义不太匹配。例如,设置 NLS_SORT= Japanese_M 意味着它将正确对拉丁语、西欧语和日语进行排序,无论任何或所有字符是否出现在可排序列中的给定字符串中。
I have a list of UTF-8 strings that I want to sort using Enumerable.OrderBy. The strings may contain any number of character sets - e.g., English, German, and Japanese, or a mix of them, even.
For example, here is a sample input list:
["東京","North 東京", "München", "New York", "Chicago", "大阪市"]
I am confused as to whether using StringComparer.CurrentCulture is the right string comparison parameter to pass to OrderBy()
. What if the current culture of the application is en-US
but I still want to sort UTF-8 data "correctly" beyond just en-US
sorting rules?
My confusion probably stems from my understanding of the NLSSORT function in Oracle that doesn't quite match up with .NET string comparison and sorting semantics. For example, setting NLS_SORT=Japanese_M means it would sort Latin, Western European, and Japanese correctly, regardless of whether any or all of the characters occur in a given string in the sortable column.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
没有一种比较适用于所有文化。
如果不需要检测语言并进行相应的选择,InvariantCulture 是您最好的选择。正如您链接的文档所述:
我添加了强调。这个例外或多或少就是你正在做的事情。
There is no one comparison which works for all cultures.
Short of detecting the language and choosing accordingly, InvariantCulture is your best bet. As the document you link notes:
I added the emphasis. That exception is more or less what you're doing.
密切关注球:您进行排序以帮助人们找到列表中的字符串。您需要一位熟练的语言学家同时了解英语、德语和日语的排序规则。有人看到你的清单的几率有多大?始终确保列表根据当地文化规则进行排序,并且排序是本地化的。
Keep your eyes on the ball: you sort to help humans find back a string in a list. You'll need a skilled linguist to know the sorting rules for English, German and Japanese at the same time. What are the odds of one laying eyes on your list? Always make sure the list is sorted according to the local culture rules and that sorting is localized.