Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,677 questions
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
I want to apply custom sort to myList after converting New_RNR_PathDic of Dictionary<string, string> type to List<KeyValuePair<string, string>> myList.
Below is my code, myList.Sort(new Sort_CSVDate());
The code could not be executed because an error occurred at this line. Is there something wrong with my code?
List<KeyValuePair<string, string>> myList = New_RNR_PathDic.ToList();
myList.Sort(new Sort_CSVDate()); //Error : cannot conver to 'System.Collections.Generic.IComparer
public class Sort_CSVDate : IComparer<object>
{
public int Compare(object x, object y)
{
//KeyValuePair<string, string>
var x1 = (KeyValuePair<string, string>)x;
var y1 = (KeyValuePair<string, string>)y;
var x2 = x1.Value;
var y2 = y1.Value;
var cast_x = x1.ToString().Substring(x1.ToString().IndexOf("-") + 1, x1.ToString().Length - x1.ToString().IndexOf("-") - 1).Replace(".csv", "");
var cast_y = y1.ToString().Substring(y1.ToString().IndexOf("-") + 1, y1.ToString().Length - y1.ToString().IndexOf("-") - 1).Replace(".csv", "");
DateTime date_x = DateTime.ParseExact(cast_x, "dd-MMM-yyyy HH.mm.ss", CultureInfo.InvariantCulture);
DateTime date_y = DateTime.ParseExact(cast_y, "dd-MMM-yyyy HH.mm.ss", CultureInfo.InvariantCulture);
return date_x.CompareTo(date_y);
}
}
To fix the compilation error, try this definition: public class Sort_CSVDate : IComparer<KeyValuePair<string, string>>.