मैं अपने SortedDictionary < के लिए अपने कस्टम IComparer उपयोग करने के लिए> की समस्या आ रही हूँ। लक्ष्य ईमेल पते को एक विशिष्ट प्रारूप ([email protected]) में कुंजी के रूप में रखना है, और अंतिम नाम से सॉर्ट करना है। जब मैं कुछ इस तरह करते हैं: मैं इस ArgumentException मिलसॉर्टेड डिक्शनरी के लिए कस्टम आईसीओएमपेयर का उपयोग कैसे करें?
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);
}
}
: An entry with the same key already exists.
जब दूसरे मद जोड़ने।
मैं पहले एक SortedDictionary के लिए एक कस्टम IComparer साथ काम किया है नहीं, और मैं अपने त्रुटि को देखने के लिए असफल, मैं गलत क्या कर रहा हूँ?
धन्यवाद। यह सरल सुरुचिपूर्ण समाधान था। –