I'm sure this is simple but it has me stumped. I want, simplified, to sort my alphabet but putting Ds between As and Bs. I think I want a custom IComparer to do this.
How would I finish this IComparer implementation to pass my Assertion? The IComparer documentation says to return less than 0 if x is < y, but does it matter how much less than zero? Scratching my head.
private static void Main(string[] args)
{
var letters = new List<string> { "A2", "E", "B1", "A1", "D", "C", "B2" };
var sorted = new List<string> { "A1", "A2", "D", "B1", "B2", "C", "E" };
letters.Sort(new MyComparer());
Assert.IsTrue(letters.SequenceEqual(sorted));
}
/// <summary>
/// Sorts D between A and B
/// </summary>
private class MyComparer : IComparer<string>
{
public int Compare(string x, string y)
{
if (string.Equals(x, "D"))
{
// return what?
}
return string.CompareOrdinal(x, y);
}
}