i'm trying to make an IComparer that will sort numbers in ascending order with the exception that any negative values (in my case, all -1's) will sort to the end. the sequence would be 0, 1, 2, 3, -1, -1, -1... but i'm wasting all dam day and getting nowhere

i only have 4 numbers so far, -1, -1, 0, 1 and i cant even get that sorted correctly without sorting twice

public class DisplayOrderSorter : IComparer<AdHocColumn>
    {
        public int Compare(Column col1, Column col2)
        {
            if (col1.DisplayOrder == col2.DisplayOrder)
                return 0;
            else if (col1.DisplayOrder < 0 || col1.DisplayOrder > col2.DisplayOrder)
                return 1;
            else //if (col1.DisplayOrder < col2.DisplayOrder)
                return -1;
        }
    }

that's the version of my icomparer that i'm on at the moment. anyone see what i'm doing wrong, besides maybe not sleeping enough... ?

Recommended Answers

All 2 Replies

You're trying to do it all at once, but it's easier to write if you split it up into parts.

First, check to see if the display orders are both positive or both negative. If they aren't, then return that the positive one is "less than" the negative one. If they are, then just use the standard comparison. If you want the negative ones to go {-1, -2, -3, ...} you'll have to reverse the comparison when they're both negative.

Like this:

if(col1.DisplayOrder == col2.DisplayOrder) // Equal.
{
    return 0;
}
else // Not equal.
{
    bool col1neg = col1.DisplayOrder < 0; // Column 1 negative?
    bool col2neg = col2.DisplayOrder < 0; // Column 2 negative?
    
    if(col1neg != col2neg) // One negative, one positive.
    {
        if(col1neg) // Column 1 is negative, column 2 is positive.
        {
            return 1; // Negative is always after positive.
        }
        else // Column 1 is positive, column 2 is negative.
        {
            return -1; // Positive is always before negative.
        }
    }
    else // Both negative or both positive.
    {
        int c = col1.DisplayOrder.CompareTo(col2.DisplayOrder);
        
        if(col1neg) // Both negative.
        {
            return -c; // Compare reversed (so you get -1, -2, -3, ...)
        }
        else // Both positive.
        {
            return c; // Compare normally
        }
    }
}

Compact version:

if(col1.DisplayOrder == col2.DisplayOrder) return 0;
else
{
    bool col1neg = col1.DisplayOrder < 0;
    bool col2neg = col2.DisplayOrder < 0;
    
    if(col1neg != col2neg) return col1neg ? 1 : -1;
    else
    {
        int c = col1.DisplayOrder.CompareTo(col2.DisplayOrder);
        return col1neg ? -c : c;
    }
}

There's probably a way to do it without the nested "if" statements, but this should be much clearer for anyone reading it.

cool
thank you
i let negatives in col2 slip by

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.