使用 StringComparison.Ordinal 对字符串进行排序的最简单方法
Easiest method to OrderBy a String using StringComparison.Ordinal
我发现了 String.CompareTo 和二进制搜索导致的错误(在我的代码中),因为我的自定义 IComparer(用于包装类型)使用 String.Compare(x, y, StringComparison.Ordinal)
.
这是因为 items.OrderBy(i => i.Name)
(其中 Name 是字符串类型)用于构建要搜索的数组使用字符串对象本身作为 IComparable - 并且具有不同的规则:
The comparison uses the current culture to obtain culture-specific information such as casing rules and the alphabetic order of individual characters. For example, a culture could specify that certain combinations of characters be treated as a single character, or uppercase and lowercase characters be compared in a particular way, or that the sorting order of a character depends on the characters that precede or follow it.
例如,{A, b, C} 使用 OrderBy-using-Default-String-Compare 排序为 [A, b, C]
,但根据 Ordinal 比较应该是 [b, A, C]
- 因为它是不是,二分查找失败了。
现在,随着 "context" 的出现,
使用与 String.Compare(.., StringComparison.Ordinal)
相同的字符串属性对对象进行排序的最简单方法(例如,无需为字符串实现自定义 IComparer)是什么?
编辑:我 [刚刚意识到我] 可以而且可能应该只使用 OrderBy(x => x, theSameComparer)
- 但假设这是不可能的,如何使用 OrderBy 获得相同的结果?
有一个预建的 StringComparer
适用 StringComparison.Ordinal
- that's StringComparer.Ordinal
:
items.OrderBy(i => i.Name, StringComparer.Ordinal)
您应该可以将 StringComparer.Ordinal
直接添加到您的 OrderBy
中。
string[] content = { "A", "b", "C", "d", "AB", "Ab" };
var ordered = content.OrderBy(o => o, StringComparer.Ordinal);
然后,一旦您遍历 ordered
,您将收到以下输出:
// Output:
A
AB
Ab
C
b
d
我相信这就是你想要的。
值得一提的是,它不仅可以用于Linq
:
,还可以用于Array
和List
排序
string[] content = { "A", "b", "C", "d", "AB", "Ab" };
Array.Sort(content);
//output: A,Ab,AB,b,C,d
Array.Sort(content, StringComparer.Ordinal);
//output: A,AB,Ab,C,b,d
var list = new List<string>{ "A", "b", "C", "d", "AB", "Ab" };
list.Sort(StringComparer.Ordinal);
//output: the same as array.srot and linq with Ordinal comparer
我发现了 String.CompareTo 和二进制搜索导致的错误(在我的代码中),因为我的自定义 IComparer(用于包装类型)使用 String.Compare(x, y, StringComparison.Ordinal)
.
这是因为 items.OrderBy(i => i.Name)
(其中 Name 是字符串类型)用于构建要搜索的数组使用字符串对象本身作为 IComparable - 并且具有不同的规则:
The comparison uses the current culture to obtain culture-specific information such as casing rules and the alphabetic order of individual characters. For example, a culture could specify that certain combinations of characters be treated as a single character, or uppercase and lowercase characters be compared in a particular way, or that the sorting order of a character depends on the characters that precede or follow it.
例如,{A, b, C} 使用 OrderBy-using-Default-String-Compare 排序为 [A, b, C]
,但根据 Ordinal 比较应该是 [b, A, C]
- 因为它是不是,二分查找失败了。
现在,随着 "context" 的出现,
使用与 String.Compare(.., StringComparison.Ordinal)
相同的字符串属性对对象进行排序的最简单方法(例如,无需为字符串实现自定义 IComparer)是什么?
编辑:我 [刚刚意识到我] 可以而且可能应该只使用 OrderBy(x => x, theSameComparer)
- 但假设这是不可能的,如何使用 OrderBy 获得相同的结果?
有一个预建的 StringComparer
适用 StringComparison.Ordinal
- that's StringComparer.Ordinal
:
items.OrderBy(i => i.Name, StringComparer.Ordinal)
您应该可以将 StringComparer.Ordinal
直接添加到您的 OrderBy
中。
string[] content = { "A", "b", "C", "d", "AB", "Ab" };
var ordered = content.OrderBy(o => o, StringComparer.Ordinal);
然后,一旦您遍历 ordered
,您将收到以下输出:
// Output: A AB Ab C b d
我相信这就是你想要的。
值得一提的是,它不仅可以用于Linq
:
Array
和List
排序
string[] content = { "A", "b", "C", "d", "AB", "Ab" };
Array.Sort(content);
//output: A,Ab,AB,b,C,d
Array.Sort(content, StringComparer.Ordinal);
//output: A,AB,Ab,C,b,d
var list = new List<string>{ "A", "b", "C", "d", "AB", "Ab" };
list.Sort(StringComparer.Ordinal);
//output: the same as array.srot and linq with Ordinal comparer