DefaultComparator

Unless I’ve missed it, this snippet could be added to Juce

/** A default operator based comparator.
     The ElementType class must overload "operator <". 
     Usage is quite simple:
     @code
          Array<ElementType> array;
          array.addSorted(DefaultComparator<ElementType>(), element);
     @endcode
*/
template <typename ElementType>
struct DefaultComparator
{
    static int compareElements (ElementType first, ElementType second)
    {
        if (first < second) return -1;
        if (second < first) return 1;
        return 0;
    }
};

Jules, if you can add this to the array class :

template<typename Comparator = DefaultComparator<ElementType> > 
void addSorted(ElementType element); 

This can even simplify our code.

That’s a very good idea. In fact, the existing IntegerElementComparator is almost exactly the same thing - I ought to clean that up and give it a more generic name. Not sure why I didn’t do so in the first place, that’s very old code…

Once again I’ve been fooled by the documentation. Searching for comparator doesn’t return this class.
Hopefully, you’ll release soon (please also update your copy of doxygen, new version have a javascript based search module).
Anyway, there’s a nice trick to use only one operator : if (first < second) elif (second < first) else equal, so it’s a small step for a class, but it’s a giant leap for class designer. :smiley:

I came across a bug in DefaultElementComparator, juce_ElementComparator.h line 252:

static int compareElements (ParameterType first, ParameterType second) { return (first < second) ? -1 : ((first < second) ? 1 : 0); }

Thanks for a fantastic framework. BTW has anyone posted a ready-made fix for displaying juce containers nicely in the Visual Studio debugger?

[quote=“fgcapo”]I came across a bug in DefaultElementComparator, juce_ElementComparator.h line 252:

CODE: SELECT ALL
static int compareElements (ParameterType first, ParameterType second)
{
return (first < second) ? -1 : ((first < second) ? 1 : 0);
}
[/quote]

…erm, that’s not what it says in my copy! Are you quoting from a really old version or something? Get up to date, there’s no excuse for not moving up to 1.52 now!