Results 1 to 11 of 11

Thread: Best way to sort one user-defined struct

  1. #1
    Join Date
    Jul 2008
    Location
    Munich
    Posts
    73
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    3

    Question Best way to sort one user-defined struct

    I have a user-defined data type, say "people". It has parameter: id (unique), number_of_books, age, weight. The data set is built.
    Now I want to have the ordered id with following criterias:
    1. number_of_books
    2. age
    3. weight

    which means, the order mainly depends on number_of_books, then age, then weight.

    How could I implement this by using QT?
    I am thinking about qSort(), but it has only one key to sort, right?
    Thanks a lot~

  2. #2
    Join Date
    Nov 2010
    Posts
    82
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanked 9 Times in 9 Posts

    Default Re: Best way to sort one user-defined struct

    qSort use the less than operator so you must implement one for your struct and the use qSort

  3. #3
    Join Date
    Jul 2008
    Location
    Munich
    Posts
    73
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    3

    Default Re: Best way to sort one user-defined struct

    Thanks. I tried, but there is error. Do you know the problem? Thanks again.
    Qt Code:
    1. qSort(before_sort.begin(), before_sort.end(), qGreater<people>);
    2.  
    3. bool MyClass::qGreater(people p1, people p2)
    4. {
    5. return true;
    6. }
    To copy to clipboard, switch view to plain text mode 

    Quote Originally Posted by Le_B View Post
    qSort use the less than operator so you must implement one for your struct and the use qSort
    Last edited by Lykurg; 2nd May 2012 at 21:12. Reason: QTCLASS is not CODE!!!

  4. #4
    Join Date
    Oct 2009
    Posts
    483
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanked 97 Times in 94 Posts

    Default Re: Best way to sort one user-defined struct

    I could not find a clear confirmation of this in the docs, but it seems to me that qGreater<T>() just calls operator>() for operands of type T. If you want to use qGreater() you have to implement the > operator for your class.

    Alternatively you can:
    - define the operator < for your class and call qSort(begin, end);
    - pick any name for your comparison function, like bool myComparisonFunction(const T &lhs, const T &rhs) and call qSort(begin, end, myComparisonFunction).

    In any case all you have to do is implement in your comparison function the lexicographical comparison you described.

  5. #5
    Join Date
    Nov 2010
    Posts
    82
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanked 9 Times in 9 Posts

    Default Re: Best way to sort one user-defined struct

    exactly in your people struct just put:

    Qt Code:
    1. bool operator <(const T& b) const
    2. {
    3. // put you code here
    4. }
    To copy to clipboard, switch view to plain text mode 

    and then use :
    Qt Code:
    1. QList<people> lst_people;
    2. qSort(lst_people);
    To copy to clipboard, switch view to plain text mode 
    Last edited by Lykurg; 2nd May 2012 at 21:15. Reason: missing [code] tags

  6. #6
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Wiki edits
    5

    Default Re: Best way to sort one user-defined struct

    or without modifying the struct use
    Qt Code:
    1. qSort(before_sort.begin(), before_sort.end(), MyClass::qGreater);
    2. bool MyClass::qGreater(const people& p1, const people& p2)
    3. {
    4. return true;
    5. }
    To copy to clipboard, switch view to plain text mode 

  7. #7
    Join Date
    Sep 2011
    Posts
    1,241
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    3
    Thanked 127 Times in 126 Posts

    Default Re: Best way to sort one user-defined struct

    are you sure that will work with member function as comparer?


    e:
    just tested, and it wont work.

    needs to be a free function. or maybe a static.
    Last edited by amleto; 2nd May 2012 at 23:27.
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  8. #8
    Join Date
    Oct 2009
    Posts
    483
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanked 97 Times in 94 Posts

    Default Re: Best way to sort one user-defined struct

    Right, it should be a standalone function or a static method.

  9. #9
    Join Date
    Jul 2008
    Location
    Munich
    Posts
    73
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    3

    Smile Re: Best way to sort one user-defined struct

    Thanks a lot for all the replies!
    The following code works. That's right, the compare method has to be static, should be not as a member function of this class.

    Qt Code:
    1. bool qGreater(people p1, people p2)
    2. {
    3. return true;
    4. }
    5.  
    6. QList<people> MyClass::sorting_people()
    7. {
    8. ...
    9. qSort(before_sort.begin(), before_sort.end(), qGreater);
    10. ...
    11. }
    To copy to clipboard, switch view to plain text mode 

  10. #10
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Wiki edits
    5

    Default Re: Best way to sort one user-defined struct

    Also member functions can be static! So instead polluting the global namespace I would at least create a namespace for comparing function. And as for your case if you have a function MyClass::sorting_people, I would probably go for a static member function called MyClass::sorting_people_helper or something like that.

  11. #11

    Default Re: Best way to sort one user-defined struct

    Hi

    I cant quite understand, I have hte following:

    QList<QWidget *> tempList = tempLegend->findChildren<QWidget *>();
    qSort(tempList.begin(), tempList.end(), qGreater);

    but this is in a PlotWindow::PlotWindow constructor. The following error is generated:

    missing template arguements before ) token.

Similar Threads

  1. How to get user-defined message?
    By ponponfish in forum Qt Programming
    Replies: 3
    Last Post: 17th May 2011, 17:19
  2. QUrl with user-defined structure
    By csoler in forum Qt Programming
    Replies: 0
    Last Post: 5th April 2010, 11:29
  3. QSqlQueryModel + user defined roles
    By NoRulez in forum Qt Programming
    Replies: 1
    Last Post: 7th December 2009, 08:29
  4. Using User Defined Libraries
    By csvivek in forum Qt Programming
    Replies: 1
    Last Post: 27th May 2008, 20:47
  5. User Defined Signal?
    By rajeshs in forum Newbie
    Replies: 2
    Last Post: 18th December 2007, 12:42

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
  •  
Qt is a trademark of The Qt Company.