Why does “-less” sort after “hello” instead of before it?

后端 未结 4 1271
心在旅途
心在旅途 2021-01-05 17:19

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 w

4条回答
  •  失恋的感觉
    2021-01-05 17:29

    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.

提交回复
热议问题