Results 1 to 5 of 5

Thread: sort

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Nov 2013
    Posts
    10
    Thanks
    8
    Platforms
    MacOS X Unix/X11 Windows Android

    Default sort

    HI,

    I have data(int, string). for ex: {(23,"z"),(23,"c"),(100,"ex"),(1,"zz"),(2,"345")}

    I want to sort combination of two keys.

    output: {(1,"zz"),(2,"345"),(23,"c"),(23,"z"),(100,"ex")}

    What is the right data structure to store two values and sort on two keys?

    Thanks.

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: sort

    Easiest way is to put the values in to instances of a class or struct and have that class implement a "less than" operator

    Qt Code:
    1. struct Data
    2. {
    3. int i;
    4.  
    5. bool operator<(const Data& other) const
    6. {
    7. // return true if "this" is less than "other", otherwise return false;
    8. }
    9. };
    To copy to clipboard, switch view to plain text mode 

    The put into a QList or QVector and use qSort()

    Cheers,
    _

  3. The following user says thank you to anda_skoa for this useful post:

    av2306 (23rd February 2014)

  4. #3
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,316
    Thanks
    315
    Thanked 870 Times in 857 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: sort

    Or try the completely heretical concept using the C++ standard library std:: pair< int, QString > instead of defining a new class. std:: pair<> already has the comparison operators defined by default (and will sort the pairs exactly the way you asked).

  5. The following user says thank you to d_stranz for this useful post:

    av2306 (23rd February 2014)

  6. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: sort

    Or (if heresy is not your thing you could use QPair the same way.

  7. The following user says thank you to ChrisW67 for this useful post:

    av2306 (23rd February 2014)

  8. #5
    Join Date
    Nov 2013
    Posts
    10
    Thanks
    8
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: sort

    Thank you all. Let me try. :-)

Similar Threads

  1. How to Sort a QModelIndexList
    By enricong in forum Qt Programming
    Replies: 2
    Last Post: 31st August 2016, 16:04
  2. 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
  3. QAbstractTableModel and sort
    By foxyproxy in forum Qt Programming
    Replies: 7
    Last Post: 25th March 2008, 09:30
  4. How to sort QMap
    By mamyte03@gmail.com in forum Qt Programming
    Replies: 2
    Last Post: 9th November 2007, 10:11
  5. When sort the lists?
    By SkripT in forum Qt Programming
    Replies: 6
    Last Post: 14th April 2006, 16:30

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.