如何为 SortedDictionary 使用自定义 IComparer?
我在为 SortedDictionary<> 使用自定义 IComparer 时遇到困难。目标是将电子邮件地址设置为特定格式 ([电子邮件受保护] ) 作为键,并按姓氏排序。 当我做这样的事情时:
public class Program
{
public static void Main(string[] args)
{
SortedDictionary<string, string> list = new SortedDictionary<string, string>(new SortEmailComparer());
list.Add("[email protected]", "value1");
list.Add("[email protected]", "value2");
foreach (KeyValuePair<string, string> kvp in list)
{
Console.WriteLine(kvp.Key);
}
Console.ReadLine();
}
}
public class SortEmailComparer : IComparer<string>
{
public int Compare(string x, string y)
{
Regex regex = new Regex("\\b\\w*@\\b",
RegexOptions.IgnoreCase
| RegexOptions.CultureInvariant
| RegexOptions.IgnorePatternWhitespace
| RegexOptions.Compiled
);
string xLastname = regex.Match(x).ToString().Trim('@');
string yLastname = regex.Match(y).ToString().Trim('@');
return xLastname.CompareTo(yLastname);
}
}
我得到这个 ArgumentException: 添加第二个项目时,已存在具有相同键的条目。
。
我之前没有使用过 SortedDictionary 的自定义 IComparer,并且我没有看到我的错误,我做错了什么?
I am having difficulties to use my custom IComparer for my SortedDictionary<>. The goal is to put email addresses in a specific format ([email protected]) as the key, and sort by last name.
When I do something like this:
public class Program
{
public static void Main(string[] args)
{
SortedDictionary<string, string> list = new SortedDictionary<string, string>(new SortEmailComparer());
list.Add("[email protected]", "value1");
list.Add("[email protected]", "value2");
foreach (KeyValuePair<string, string> kvp in list)
{
Console.WriteLine(kvp.Key);
}
Console.ReadLine();
}
}
public class SortEmailComparer : IComparer<string>
{
public int Compare(string x, string y)
{
Regex regex = new Regex("\\b\\w*@\\b",
RegexOptions.IgnoreCase
| RegexOptions.CultureInvariant
| RegexOptions.IgnorePatternWhitespace
| RegexOptions.Compiled
);
string xLastname = regex.Match(x).ToString().Trim('@');
string yLastname = regex.Match(y).ToString().Trim('@');
return xLastname.CompareTo(yLastname);
}
}
I get this ArgumentException:An entry with the same key already exists.
when adding the second item.
I haven't worked with a custom IComparer for a SortedDictionary before, and I fail to see my error , what am I doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好吧,我还没有拆开你的比较器 - 但看起来它只是按姓氏进行比较,而你试图添加相同的姓氏 (johansson) 两次。 应该给你一个
ArgumentException
。您希望发生什么 - 您希望比较者做什么?
也许您想按姓氏然后按名字排序?这样,您就可以拥有两个姓氏相同但名字不同的电子邮件地址,并且它们仍然一起出现在字典中,并按名字排序。
Well, I haven't taken apart your comparer - but it looks like it's just comparing by last name, and you're trying to add the same last name (johansson) twice. That should give you an
ArgumentException
.What did you want to happen - and what do you want your comparer to do?
Perhaps you want to sort by last name and then first name? That way you can have two email addresses with the same last name but different first names, and have them still be in the dictionary together, ordered by first name.
如果两个姓氏相等,则比较整个电子邮件,例如:
实际上,sorteddictionary比较也用于区分keys*,因此您必须指定完整的比较(不仅仅是您的排序策略)
编辑:
* 我的意思是在sortedDictionary中,如果比较器给出0,则2个键相等
If the 2 lastNames are equal then compare for example the whole email like:
Actually, sorteddictionary comparison is also used to distinguish amongst keys* , so you must specify a complete comparison (not only your sorting strategy)
EDIT:
* I mean in sortedDictionary 2 keys are equal if Comparer gives 0