How do you define a custom comparer

 

Hi. I am trying and failing to create custom comparers for generic collections of objects in MQL5. Can someone please help me work out what I'm doing wrong?

Here is a minimal example of what I've tried.

#property indicator_chart_window

#include <Generic/ArrayList.mqh>
#include <Generic/Interfaces/IComparer.mqh>

class Integer
{
    private:
        int m_value;
    public:
        Integer(const int value) {m_value = value;}
        int Value() {return m_value;}
};

class Cmp : IComparer<Integer *>
{
    int Compare(Integer *x, Integer *y) {return x.Value() - y.Value();}
};

int OnInit()
{
    CArrayList<Integer *> test;
    test.Add(new Integer(2));
    test.Add(new Integer(1));
    test.Add(new Integer(3));
    test.Sort(new Cmp());
    for (int i = 0; i < test.Count(); i++)
    {
        Integer * elt = NULL;
        if (test.TryGetValue(i, elt))
        {
            PrintFormat("test[%d]=%d", i, elt.Value());
        }
    }
    return INIT_SUCCEEDED;
}

int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
{
    return 0;
}

From my understanding of the MQL5 library and templates, this should work. However, the line I've highlighted generates a compiler error:

'<' - template mismatch Comparer.mq5    26      22
'Sort' - no one of the overloads can be applied to the function call    Comparer.mq5    26      10
could be one of 3 function(s)   Comparer.mq5    26      10
   bool CArrayList<Integer*>::Sort()    ArrayList.mqh   70      22
   bool CArrayList<Integer*>::Sort(IComparer<Integer*>*)        ArrayList.mqh   71      22
   bool CArrayList<Integer*>::Sort(const int,const int,IComparer<Integer*>*)    ArrayList.mqh   72      22

I thought my code matches the second case exactly, but obviously I'm wrong.

Any help would be greatly appreciated. Thanks!

 
Bill M:

Hi. I am trying and failing to create custom comparers for generic collections of objects in MQL5. Can someone please help me work out what I'm doing wrong?

Here is a minimal example of what I've tried.

From my understanding of the MQL5 library and templates, this should work. However, the line I've highlighted generates a compiler error:

I thought my code matches the second case exactly, but obviously I'm wrong.

Any help would be greatly appreciated. Thanks!

So turns out the problem was from inheritance and member visibility. I'm not sure those compiler errors were that informative...

I'll include the fix for the reference of others in case someone also hits against this. The class Cmp needed to inherit with public visibility and then all works as desired.

class Cmp : public IComparer<Integer *>
{
    int Compare(Integer *x, Integer *y) {return x.Value() - y.Value();}
};
 
Your topic has been moved to the section: Technical Indicators
Please consider which section is most appropriate — https://www.mql5.com/en/forum/172166/page6#comment_49114893