key*_*oss 1 c# logic enums .net-4.0 sorteddictionary
我正在试图弄清楚如何创建一个排序字典,其中键以非字母方式排序.有没有办法可以定义我想要排序的方式?
例如,键可能按如下顺序排列:
AAA1X
AAB1Y
AAC1Y
AAA2X
AAB2Y
AAC2X
Run Code Online (Sandbox Code Playgroud)
虽然前三个字母是按字母顺序排列的,但如果按其排序,它会将它们按错误的顺序排列(由于数字).另请注意,末尾有一个X或Y. 在代码中,只会有X或Y.
即使我可以为所有可能组合的排序编写枚举,我也愿意这样做,但我不知道如何使用排序字典和枚举......
我知道这有点模糊,但任何帮助都会非常感激!
干杯!
Ada*_*son 16
其中一个构造函数的SortedDictionary<TKey, TValue>需要的IComparer<TKey>,在这里你可以指定该字典将用于排序的自定义比较器类.
public class CustomComparer : IComparer<string>
{
public int Compare(string x, string y)
{
// do your own comparison however you like; return a negative value
// to indicate that x < y, a positive value to indicate that x > y,
// or 0 to indicate that they are equal.
}
}
...
SortedDictionary<string, object> dict =
new SortedDictionary<string, object>(new CustomComparer());
Run Code Online (Sandbox Code Playgroud)