Search code examples
c#sortingicomparer

How can I change the sort order using an IComparer in .NET


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);
    }
}

Solution

  • but does it matter how much less than zero

    Nope, not at all.

    Basically each comparison has to give a single result from three options:

    • First value is less than second value
    • Values are equal
    • First value is more than second value

    So to make "D" come between "A" and "B" you'd use something like:

    public int Compare(string x, string y)
    {
        if (x == y)
        {
            return 0;
        }
        if (x == "D")
        {
            // Unless y is *actually* "B", we can just
            // pretend that x is "B". (So it will be after any "A", but before
            // any other "Bxyz".)
            if (y == "B")
            {
                return -1;
            }
            return "B".CompareTo(y);
        }
        // Ditto, basically. Alternatively you could call Compare(y, x)
        // and invert the result, but *don't* just negate it, as it does the
        // wrong thing with int.MinValue...
        if (x == "D")
        {
            if (x == "B")
            {
                return 1;
            }
            return x.CompareTo("B");
        }
        return x.CompareTo(y);
    }