Simon Fell > Its just code : Wednesday, April 24, 2002

Simon Fell > Its just code

Wednesday, April 24, 2002

Back to IComparer.Compare, I'd prefer it if it was

    public bool Compare ( object a, object b )

returns true if a precedes b in a Strict Weak Order.
< 10:38:00 PM  # more elsewhere > The Messiah - You're Going Insane

A number of people have asked why I don't like IComparer.Compare. What I don't like is that I have to implement tests for <, = and >. STL uses Strict Weak Ordering which only requires a < operator (or a binary predicate equivilent). This has nothing to do with STL's use of templates, and in fact the implementation of IComparer.Compare I wrote only needs a < operator for the type it delegates to.

    public int Compare ( object a, object b )
    {
        DateTime l = ((sample)a).date ;
        DateTime r = ((sample)b).date ;
        if ( l < r )
            return -1;
        if ( r < l )
            return 1;
        return 0 ;
    }


< 7:46:58 PM  # more elsewhere > The Messiah - Temple Of Dreams