Results 1 to 5 of 5

Thread: Sorting a StringList

  1. #1
    Join Date
    Jan 2006
    Location
    Edmonton, Canada
    Posts
    101
    Thanks
    13
    Thanked 6 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Sorting a StringList

    Hello

    I am trying to write the sort function for a QStringList object. I know that there is already a built-in function, but it is not sufficient for what I need because it only sorts ascending.

    Here is the code I am using:
    Qt Code:
    1. QStringList MyClass::sort(QStringList stringList, Qt::SortOrder order)
    2. {
    3. if ( order == Qt::AscendingOrder )
    4. {
    5. qSort(stringList.begin(), stringList.end(), caseInsensitiveLessThan);
    6. }
    7. else
    8. {
    9. qSort(stringList.begin(), stringList.end(), caseInsensitiveGreaterThan);
    10. }
    11. return stringList;
    12. }
    13.  
    14. bool MyClass::caseInsensitiveLessThan(const QString &s1, const QString &s2)
    15. {
    16. return s1.toLower() < s2.toLower();
    17. }
    18.  
    19. bool MyClass::caseInsensitiveGreaterThan(const QString &s1, const QString &s2)
    20. {
    21. return s1.toLower() > s2.toLower();
    22. }
    To copy to clipboard, switch view to plain text mode 

    This is based on examples provided in the QtAlgorithms documentation. The problem I am having is that when I try to compile I get a compile-time error:

    term does not evaluate to a function taking 2 arguments
    c:\Qt\4.1.0\include\QtCore\../../src\corelib\tools\qalgorithms.h(141) : see reference to function template instantiation 'void QAlgorithmsPrivate::qSortHelper<BiIterator,T,LessT han>(BiIterator,BiIterator,const T &,LessThan)' being compiled
    with
    [
    BiIterator=QList<QString>::iterator,
    T=QString,
    LessThan=bool (__thiscall MyClass::* )(const QString &,const QString &)
    ]
    myclass.cpp(90) : see reference to function template instantiation 'void qSort<QList<T>::iterator,bool(__thiscall MyClass::* )(const QString &,const QString &)>(BiIterator,BiIterator,LessThan)' being compiled
    with
    [
    T=QString,
    BiIterator=QList<QString>::iterator,
    LessThan=bool (__thiscall MyClass::* )(const QString &,const QString &)
    ]


    I am new to Qt and my C++ is very rusty. I would appreciate some advice with regards to debugging this problem and/or interpreting this error msg.

    Thanks in advance,

    Jimmy

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Sorting a StringList

    Qt Code:
    1. qSort(stringlist.begin(), stringlist.end(), qGreater<QString>());
    To copy to clipboard, switch view to plain text mode 

    This might be a bit more simple.

  3. #3
    Join Date
    Jan 2006
    Location
    Edmonton, Canada
    Posts
    101
    Thanks
    13
    Thanked 6 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Sorting a StringList

    That is simpler and does solve the error - thank you wysota.

    However, I would still like to know what the problem was with my origional code that would cause it not to compile so I can understand it better.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Sorting a StringList

    Quote Originally Posted by Jimmy2775
    However, I would still like to know what the problem was with my origional code that would cause it not to compile so I can understand it better.
    You were trying to use a pointer to a method instead of a pointer to a standalone function.

    This should work:
    Qt Code:
    1. bool caseInsensitiveLessThan(const QString &s1, const QString &s2)
    2. {
    3. return s1.toLower() < s2.toLower();
    4. }
    5.  
    6. bool caseInsensitiveGreaterThan(const QString &s1, const QString &s2)
    7. {
    8. return s1.toLower() > s2.toLower();
    9. }
    10.  
    11. QStringList MyClass::sort(QStringList stringList, Qt::SortOrder order)
    12. {
    13. if ( order == Qt::AscendingOrder )
    14. {
    15. qSort(stringList.begin(), stringList.end(), caseInsensitiveLessThan);
    16. }
    17. else
    18. {
    19. qSort(stringList.begin(), stringList.end(), caseInsensitiveGreaterThan);
    20. }
    21. return stringList;
    22. }
    To copy to clipboard, switch view to plain text mode 

  5. #5
    Join Date
    Jan 2006
    Location
    Edmonton, Canada
    Posts
    101
    Thanks
    13
    Thanked 6 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Sorting a StringList

    OK - I understand. Thank you.

Similar Threads

  1. Refresh QTableView after sorting
    By araglin in forum Newbie
    Replies: 4
    Last Post: 18th December 2008, 22:13
  2. Qt4: Sorting QAbstractItemVew inherited view
    By nando in forum Qt Programming
    Replies: 3
    Last Post: 12th February 2008, 18:30
  3. Column Sorting
    By sumsin in forum Qt Programming
    Replies: 1
    Last Post: 16th June 2006, 07:48
  4. QT4: Sorting in QTreeWidget (subclass)
    By Michiel in forum Qt Programming
    Replies: 21
    Last Post: 29th March 2006, 18:08
  5. [QT4] QTreeView, QAbstractItemModel and sorting
    By KShots in forum Qt Programming
    Replies: 3
    Last Post: 24th March 2006, 20:16

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.