# DefaultComparator

**URL:** <https://forum.juce.com/t/defaultcomparator/5188>\
**Category:** General JUCE discussion\
**Created:** [April 22, 2010, 11:40am UTC](https://forum.juce.com/t/defaultcomparator/5188 "2010-04-22T11:40:47Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![X-Ryl669](https://avatars.discourse-cdn.com/v4/letter/x/a5b964/32.png) [@X-Ryl669](https://forum.juce.com/u/X-Ryl669)\
**Post date:** [April 22, 2010, 11:40am UTC](https://forum.juce.com/t/defaultcomparator/5188/1 "2010-04-22T11:40:47Z")

</div>

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

```auto
/** 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 :

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

This can even simplify our code.

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://forum.juce.com/u/jules)\
**Post date:** [April 22, 2010, 3:20pm UTC](https://forum.juce.com/t/defaultcomparator/5188/2 "2010-04-22T15:20:26Z")

</div>

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…

---

<div class="post-metadata">

**Author:** ![X-Ryl669](https://avatars.discourse-cdn.com/v4/letter/x/a5b964/32.png) [@X-Ryl669](https://forum.juce.com/u/X-Ryl669)\
**Post date:** [April 22, 2010, 4:00pm UTC](https://forum.juce.com/t/defaultcomparator/5188/3 "2010-04-22T16:00:17Z")

</div>

> [@jules](#):
>
> In fact, the existing IntegerElementComparator is almost exactly the same thing

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. 😃

---

<div class="post-metadata">

**Author:** ![fgcapo](https://avatars.discourse-cdn.com/v4/letter/f/ea5d25/32.png) [@fgcapo](https://forum.juce.com/u/fgcapo)\
**Post date:** [December 23, 2010, 8:57am UTC](https://forum.juce.com/t/defaultcomparator/5188/4 "2010-12-23T08:57:06Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://forum.juce.com/u/jules)\
**Post date:** [December 23, 2010, 9:24am UTC](https://forum.juce.com/t/defaultcomparator/5188/5 "2010-12-23T09:24:36Z")

</div>

[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!
