How to use custom IComparer for SortedDictionary?

前端 未结 2 672
不思量自难忘°
不思量自难忘° 2021-01-11 17:30

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 t

2条回答
  •  清歌不尽
    2021-01-11 17:34

    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

提交回复
热议问题