在这种情况下,StringComparer.CurrentCulture 是正确的选择吗?

发布于 2024-08-16 21:12:09 字数 835 浏览 2 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

关于从前 2024-08-23 21:12:09

没有一种比较适用于所有文化。

如果不需要检测语言并进行相应的选择,InvariantCulture 是您最好的选择。正如您链接的文档所述:

不要:在大多数情况下使用基于 StringComparison.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:

DON'T: Use StringComparison.InvariantCulture-based string operations in most cases; one of the few exceptions would be persisting linguistically meaningful but culturally-agnostic data.

I added the emphasis. That exception is more or less what you're doing.

甜是你 2024-08-23 21:12:09

密切关注球:您进行排序以帮助人们找到列表中的字符串。您需要一位熟练的语言学家同时了解英语、德语和日语的排序规则。有人看到你的清单的几率有多大?始终确保列表根据当地文化规则进行排序,并且排序是本地化的。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文