Element Comparator

Hello,

For the first time I am using JUCE’s ElementComparator template. I have created a Compartor class which is pretty much a copy-past of Default Comparator.

> template <class ElementType>
> 
> class FrameComparator
> 
> {
> 
> private:
> 
> 	using ParameterType = typename TypeHelpers::ParameterType<ElementType>::type;
> 
> 
> 
> public:
> 
> 	static int compareElements(ParameterType first, ParameterType second)
> 
> 	{
> 
> 		if (first.getID() < second.getID())
> 			return -1;
> 		else if (first.getID() > second.getID())
> 			return 1;
> 		else // if a == b
> 			return 0;
> 		return 0;
> 
> 	}
> 
> };

I want to use it to compare TreeViewItems. I have a FrameExplorerItem which is derived from TreeViewItem and has a getID() method.
I create an instance of my comparator and then pass it to sort method.

> FrameComparator<FrameExplorerItem> comp;
> explorerTree.getRootItem()->sortSubItems(comp);

I get the following error:

Error C2664 ‘int FrameComparator::compareElements(const FrameExplorerItem &,const FrameExplorerItem &)’: cannot convert argument 1 from ‘Type’ to ‘const FrameExplorerItem &’

I have tried multiple things but I just dont get it. Any help will be much appreciated! Thanks.
Cila.

TBH unless you really need to use this, you’re better off doing sorting using standard C++ practice with std::sort and lambdas, std::algorithm, etc.

I originally wrote the ElementComparator stuff long before lambdas existed, so it’s pretty clunky and probably will be deprecated one day.

1 Like

Absolutely follow Jules’ advice, but your current error message is, you are implementing the arguments as by-copy types, versus the algorithm seems to expect it to be done by const references:

static int compareElements (const ParameterType& first, const ParameterType& second)

Btw. your getID() needs to be a const method for that to work…

Hope that helps

1 Like

Thank you for a quick reply, I will do it as Jules advised :slight_smile: