如何为 SortedDictionary 使用自定义 IComparer?

发布于 2024-08-30 09:04:36 字数 1647 浏览 10 评论 0原文

我在为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

戏蝶舞 2024-09-06 09:04:37

好吧,我还没有拆开你的比较器 - 但看起来它只是按姓氏进行比较,而你试图添加相同的姓氏 (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.

荒路情人 2024-09-06 09:04:36

如果两个姓氏相等,则比较整个电子邮件,例如:

int comp = xLastname.CompareTo(yLastname);
if (comp == 0)
   return x.CompareTo(y);
return comp;

实际上,sorteddictionary比较也用于区分keys*,因此您必须指定完整的比较(不仅仅是您的排序策略)

编辑:
* 我的意思是在sortedDictionary中,如果比较器给出0,则2个键相等

If the 2 lastNames are equal then compare for example the whole email like:

int comp = xLastname.CompareTo(yLastname);
if (comp == 0)
   return x.CompareTo(y);
return comp;

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文