ElementComparator usage annoyance


#1

The ElementComparator is passed by non-const reference in the Array classes, making this code unusable:

sorted.addSorted (ValidatorInfoCompare(), arrayToSort [i]);

Instead, this workaround is needed:

ValidatorInfoCompare compare;
sorted.addSorted (compare, arrayToSort [i]);

This isn’t very JUCE like! Jules I know you’ve got higher standards!


#2

It’s many many years since I wrote that, but am fairly certain I hit problems in making it const. I’d certainly prefer to have a const object there.

There’ll be some comparators that need to be mutable in order to work - e.g. I’ve written a few comparators myself which use an internal map to cache properties of the objects that they’re comparing. I guess you could argue that their members could be mutable, but it seems like that’d be working around a problem that shouldn’t exist.

Actually… Could probably just have two sort methods that take const/non-const references. That’d work, but is more duplicated code!


#3

We tried that last year and ran into problems