Problem with qSort of QList<CustomType>



  • Hello, trying to sort QList <MyType> list. Doing this:

    qSort(list.begin(),list.end(),sortFunc);
    ....
    bool sortFunc(const MyType1& mt1, const MyType2& mt2){
    return false;
    }
    

    So the first line of code above produce error:
    error: C2064: term does not evaluate to a function taking 2 arguments.
    What's the problem guys? I've already successfully sorted QStringList that way btw. Thanks in advance!



  • Hi,
    your sort function looks ok. Please make sure, though, that it is not a member function of some class and you should declare it before you call qSort.
    -Michael.



  • One small thing to check: are you defining / declaring the sortFunc function before or after the qSort invocation? ie if the code you've shown above is all within one file, and in the order you've shown, then try moving the sortFunc function definition above the qSort call in the file.

    Cheers.


  • Qt Champions 2016

    Let's see ... MSDN also provides comprehensive examples, by the way ...



  • Big thanks guys, next time i'll google better! Making the function static solved my problem. Thanks again for help guys!



  • @Paul-Colby said in Problem with qSort of QList<CustomType>:

    One small thing to check: are you defining / declaring the sortFunc function before or after the qSort invocation? ie if the code you've shown above is all within one file, and in the order you've shown, then try moving the sortFunc function definition above the qSort call in the file.

    Cheers.

    You were absolutely right. Thanks!


Log in to reply
 

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