Results 1 to 6 of 6

Thread: Sorting a QList containing structured datas

  1. #1
    Join Date
    Aug 2008
    Posts
    10
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Sorting a QList containing structured datas

    Hi everybody,

    I am sorry for posting what is probably a stupid question but I failed to find by myself the answer... sorting a Qlist with qSort() is tricky when the list only contains a single variable. But using a structure, it failed since the qSort() algorithm did not knows which field in the structure is to be sorted. Here is my code :

    Qt Code:
    1. struct myData{
    2. QString namefile;
    3. int filesize;
    4. };
    5.  
    6. QList <myData> list;
    7. populatelist();
    8. ...
    To copy to clipboard, switch view to plain text mode 

    now how to sort list by namefile or filesize ?
    I presume I need some pointers, but as newbie, I turn around it unsuccessfully. Thank you for any help.
    Last edited by jpn; 17th December 2008 at 20:05. Reason: missing [code] tags

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,518 Times in 1,388 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Sorting a QList containing structured datas

    See qSort() docs. You have to implement the operator<() or a LessThan function.
    J-P Nurmi

  3. #3
    Join Date
    Dec 2008
    Location
    chinese
    Posts
    47
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Sorting a QList containing structured datas

    Quote Originally Posted by jean View Post
    Hi everybody,

    I am sorry for posting what is probably a stupid question but I failed to find by myself the answer... sorting a Qlist with qSort() is tricky when the list only contains a single variable. But using a structure, it failed since the qSort() algorithm did not knows which field in the structure is to be sorted. Here is my code :

    Qt Code:
    1. struct myData{
    2. QString namefile;
    3. int filesize;
    4. };
    5.  
    6. QList <myData> list;
    7. populatelist();
    8. ...
    To copy to clipboard, switch view to plain text mode 

    now how to sort list by namefile or filesize ?
    I presume I need some pointers, but as newbie, I turn around it unsuccessfully. Thank you for any help.
    follow:
    myData md;
    md.namefile="test";
    md.filesize=100;
    list.appand(md);

  4. #4
    Join Date
    Aug 2008
    Posts
    10
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Sorting a QList containing structured datas

    Thank you for replying but none of these answers are usefull:

    to JPN : the Trolltech doc for qSort() only treat single variable QList. Operators are presented for performing special sortings.

    to yunpeng880 : my problem is not how to store structured datas in a QList class, but how to sort a field from a structured data once it has been stored in a QList.

    In any case, thanks for your replies.

  5. #5
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,518 Times in 1,388 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Sorting a QList containing structured datas

    Quote Originally Posted by jean View Post
    to JPN : the Trolltech doc for qSort() only treat single variable QList. Operators are presented for performing special sortings.
    In your case QList deals with myData struct. QList doesn't care at all what the composition of that type. To be able to use qSort(), you need to implement either

    1) myData::operator<():
    Qt Code:
    1. struct myData {
    2. bool operator<(const myData& other) const {
    3. return namefile < other.namefile; // sort by namefile
    4. }
    5. ...
    6. };
    7.  
    8. // usage:
    9. QList<myData> list;
    10. ...
    11. qSort(list);
    To copy to clipboard, switch view to plain text mode 

    OR

    2) a LessThan function which does the sorting instead of using operator<():
    Qt Code:
    1. bool namefileLessThan(const myData &d1, const myData &d2)
    2. {
    3. return d1.namefile < d2.namefile; // sort by namefile
    4. }
    5.  
    6. // usage:
    7. QList<myData> list;
    8. ...
    9. qSort(list.begin(), list.end(), namefileLessThan);
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  6. The following user says thank you to jpn for this useful post:

    jean (18th December 2008)

  7. #6
    Join Date
    Aug 2008
    Posts
    10
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Sorting a QList containing structured datas

    JP : Thank you very much for your concise and helpfull help. It works and you have teached me something new.
    Jean.

Similar Threads

  1. Sorting using qSort(), - if QList contains POINTERS
    By joseph in forum Qt Programming
    Replies: 13
    Last Post: 18th August 2013, 19:55

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.