排序列表/ SortedDictionary怪异的行为怪异、行为、列表、SortedDictionary

2023-09-03 01:32:42 作者:只是聊聊不谈感情

任何人都可以解释为什么这code:

 昏暗的数据作为新的排序列表(StringComparer.InvariantCultureIgnoreCase)
Data.Add(AB,48)
Data.Add(AC,48)
Data.Add(A-D,48)
Data.Add(A-,48)
 

生成排序列表顺序如下:

  A-
AB
AC
广告
 

预期(逻辑真想)的顺序是:

  A-
广告
AB
AC
 
最新 2022年美国最优职业排行,学这些专业更容易找到好工作

解决方案

 昏暗的数据作为新的排序列表(StringComparer.InvariantCultureIgnoreCase)
 

我认为这个问题是与指定的排序规则。

修改 InvariantCultureIgnoreCase OrdinalIgnoreCase 解决问题

 昏暗的数据作为新的排序列表(StringComparer.OrdinalIgnoreCase)
 

下面是演示

Can anybody explain why this code:

Dim Data As New SortedList(StringComparer.InvariantCultureIgnoreCase)
Data.Add("AB", 48)
Data.Add("AC", 48)
Data.Add("A-D", 48)
Data.Add("A-", 48)

Generates sorted list with following order:

A-  
AB  
AC  
A-D

Expected (logical and really wanted) order is:

A-
A-D
AB
AC

解决方案

Dim Data As New SortedList(StringComparer.InvariantCultureIgnoreCase)

I think the problem is with the sort rules specified.

Changing InvariantCultureIgnoreCase to Ordinal or OrdinalIgnoreCase solves the problem

Dim Data As New SortedList(StringComparer.OrdinalIgnoreCase)

Here is the Demo