Using qGreater<T> with custom objects ...



  • Hello,

    How would one go about using qGreater<T> from QtAlgorithms with a custom object?

    Do I have to implement a specific operator?

    Thank you.


  • Lifetime Qt Champion

    Hi,

    You need to implement the corresponding operator > for your custom object. If you are using Qt 5, you are encourage to use std::greater directly



  • That member is obsolete in Qt 5.4:

    http://doc.qt.io/qt-5/qtalgorithms-obsolete.html

    Anyway, you should implement your own function to compare your objects. Here you have an example:

    @bool caseInsensitiveLessThan(const QString &s1, const QString &s2)
    {
    return s1.toLower() < s2.toLower();
    }

    int doSomething()
    {
    QStringList list;
    list << "AlPha" << "beTA" << "gamma" << "DELTA";
    qSort(list.begin(), list.end(), caseInsensitiveLessThan);
    // list: [ "AlPha", "beTA", "DELTA", "gamma" ]
    }@



  • Thanks, I wanted to use a "greater than" in conjunction with a sorting algorithm; should I only use "std" style iterators?

    Also, can I use the std sorting algorithm with a QVector?


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.