At least 100000 comparisions was needed when I tried to sort with IComparer<string>
.
Why?
public class PeriodComparer: IComparer<string>
{
int num = 0;
public int Compare(string x, string y)
{
var year1 = int.Parse(x.Substring(2));
var year2 = int.Parse(y.Substring(2));
if (year1 < year2)
return -1;
if (year1 > year2)
return 1;
var season1 = x.Substring(0, 2);
return season1 == "VT" ? -1 : 1;
}
}
I tried to sort an array of strings with it like
var strings = new []
{"VT2010", "HT2010",
"VT2011", "HT2011",
"VT2012", "HT2012",
"VT2013", "HT2013",
"VT2014", "HT2014",
"VT2015", "HT2015",
"VT2016", "HT2016",
"VT2017", "HT2017"};
var comparer = new PeriodComparer();
var orderedPeriodNames = strings.OrderBy(x => x, comparer).ToList();
expecting the strings to sort first with respect to year and then with respect to VT and HT. (This means that the input in this particular case is already sorted).
However, the execution stalled so I was putting a counter in that Compare function like
public class PeriodComparer: IComparer<string>
{
int num = 0;
public int Compare(string x, string y)
{
if (++num >= 100000)
{
// setting a breakpoint here
}
var year1 = int.Parse(x.Substring(2));
var year2 = int.Parse(y.Substring(2));
if (year1 < year2)
return -1;
if (year1 > year2)
return 1;
var season1 = x.Substring(0, 2);
return season1 == "VT" ? -1 : 1;
}
}
The breakpoint was hit so at least 100000 comparisions seems to be needed.