Results 1 to 5 of 5

Thread: sort multi in class

  1. #1
    Join Date
    Jan 2012
    Posts
    4
    Qt products
    Qt4
    Platforms
    Windows

    Question sort multi in class

    Hi everyone,
    I have question about sort data in class. i need sort value X and Y
    Qt Code:
    1. class PointMatch {
    2. public :
    3. int X;
    4. int Y;
    5. QString Name;
    6. };
    To copy to clipboard, switch view to plain text mode 

    In list have data before sort :
    [2,5,'A']
    [1,2,'B']
    [1,3,'C']
    [2,3,'D']
    [1,1,'E']

    I need after sort X and Y.

    [1,1,'E']
    [1,2,'B']
    [1,3,'C']
    [2,3,'D']
    [2,5,'A']

    If have sample code. help tell me.
    Thank you.

  2. #2
    Join Date
    Jul 2012
    Posts
    123
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    4
    Thanked 2 Times in 2 Posts

    Default Re: sort multi in class


  3. #3
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,540
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanked 284 Times in 279 Posts

    Default Re: sort multi in class

    PointMatch class must define operator <() and then you can use the qSort function

  4. #4
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Thanks
    3
    Thanked 453 Times in 435 Posts
    Wiki edits
    15

    Default Re: sort multi in class

    Do you want to use Qt sort algorithms? if Yes then this is one way
    Qt Code:
    1. #include <QList>
    2. #include <QString>
    3.  
    4. #include <iostream>
    5.  
    6. class PointMatch
    7. {
    8. public :
    9. PointMatch(int x, int y, const QString & name) : X(x), Y(y), Name(name) { }
    10. PointMatch(const PointMatch & p) : X(p.X), Y(p.Y), Name(p.Name) { }
    11.  
    12. int X;
    13. int Y;
    14. QString Name;
    15. };
    16.  
    17. bool PointMatchLessThan(const PointMatch & p1, const PointMatch & p2)
    18. {
    19. if(p1.X < p2.X)
    20. return true;
    21. else if(p1.X == p2.X)
    22. {
    23. if(p1.Y < p2.Y)
    24. return true;
    25. else
    26. return false;
    27. }
    28. return false;
    29. }
    30.  
    31. int main()
    32. {
    33. QList<PointMatch> points;
    34.  
    35. points.append(PointMatch(2, 5, "A"));
    36. points.append(PointMatch(1, 2, "B"));
    37. points.append(PointMatch(1, 3, "C"));
    38. points.append(PointMatch(2, 3, "D"));
    39. points.append(PointMatch(1, 1, "E"));
    40.  
    41. foreach(PointMatch point, points)
    42. std::cout << "[" << point.X << ", " << point.Y << ", " << point.Name.toStdString() << "]" << std::endl;
    43.  
    44. qSort(points.begin(), points.end(), PointMatchLessThan);
    45.  
    46. std::cout << std::endl;
    47.  
    48. foreach(PointMatch point, points)
    49. std::cout << "[" << point.X << ", " << point.Y << ", " << point.Name.toStdString() << "]" << std::endl;
    50. }
    To copy to clipboard, switch view to plain text mode 

    other way is to implement a less than operator for the class
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  5. #5
    Join Date
    Jan 2012
    Posts
    4
    Qt products
    Qt4
    Platforms
    Windows

    Thumbs up Re: sort multi in class

    To. Santosh Reddy, Thank you very much.

Similar Threads

  1. show object (scene) in widget in class class mainwindow
    By rimie23 in forum Qt Programming
    Replies: 8
    Last Post: 1st May 2012, 16:15
  2. Replies: 1
    Last Post: 20th January 2012, 04:39
  3. Replies: 7
    Last Post: 18th August 2011, 14:43
  4. How to sort a Qlist AND get a sort key?
    By agerlach in forum Qt Programming
    Replies: 3
    Last Post: 26th July 2010, 18:44
  5. Replies: 3
    Last Post: 27th December 2008, 19:34

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.