Search code examples
c#icomparersorteddictionary

How to use custom IComparer for SortedDictionary?


I am having difficulties to use my custom IComparer for my SortedDictionary<>. The goal is to put email addresses in a specific format (firstnam.lastname@domain.com) 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("a.johansson@domain.com", "value1");
    list.Add("b.johansson@domain.com", "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?


Solution

  • 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