Results 1 to 14 of 14

Thread: how to sort a list by one of its items ?! [solved]

  1. #1
    Join Date
    Jan 2010
    Posts
    18
    Thanks
    11
    Qt products
    Qt4
    Platforms
    Unix/X11

    Question how to sort a list by one of its items ?! [solved]

    Hey Guys

    I got this :
    Qt Code:
    1. struct wordItem {
    2. QString word;
    3. int count;
    4. };
    5.  
    6. QList <wordItem> list;
    To copy to clipboard, switch view to plain text mode 

    now I want to know what is the fastest way to sort the list by wordItem.count ?!
    Is there any function in Qt or C++ to do this for me ?!

    Thank you for your time
    Last edited by gbmtoday; 2nd January 2011 at 21:38.

  2. #2
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: how to sort a list by one of its items ?!

    You can use qSort

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

    gbmtoday (2nd January 2011)

  4. #3
    Join Date
    Jan 2010
    Posts
    18
    Thanks
    11
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: how to sort a list by one of its items ?!

    Here's my code

    Qt Code:
    1. Qlist <WordItem> list;
    2.  
    3. qStableSort(list.begin(),list.end(),compareWordItem);
    4.  
    5. bool compareWordItem ( const WordItem &w1 , const WordItem &w2 )
    6. {
    7. return w1.count > w2.count;
    8. }
    To copy to clipboard, switch view to plain text mode 

    but I get this :
    error: no matching function for call to ‘qStableSort(QList<WordItem>::iterator, QList<WordItem>::iterator, <unresolved overloaded function type>)’

  5. #4
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: how to sort a list by one of its items ?!

    Did you #include <QtAlgorithms>?

  6. #5
    Join Date
    Jan 2010
    Posts
    18
    Thanks
    11
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: how to sort a list by one of its items ?!

    Quote Originally Posted by Zlatomir View Post
    Did you #include <QtAlgorithms>?
    I included it but it did not make any differnce .

  7. #6
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: how to sort a list by one of its items ?!

    Did you create a function prototype of the sort function before using it?

  8. The following user says thank you to tbscope for this useful post:

    gbmtoday (2nd January 2011)

  9. #7
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: how to sort a list by one of its items ?!

    Also the compare function/functor should be declared before calling qSort
    Qt Code:
    1. bool compareWordItem ( const WordItem &w1 , const WordItem &w2 )
    2. {
    3. return w1.count > w2.count;
    4. }
    5. qStableSort(list.begin(),list.end(),compareWordItem);
    To copy to clipboard, switch view to plain text mode 

    LE: you declare/define compareWordItem outside of any function (including main)

  10. The following user says thank you to Zlatomir for this useful post:

    gbmtoday (2nd January 2011)

  11. #8
    Join Date
    Jan 2010
    Posts
    18
    Thanks
    11
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: how to sort a list by one of its items ?!

    Quote Originally Posted by tbscope View Post
    Did you create a function prototype of the sort function before using it?
    what do you mean ?

    I just did what I said above !

    Quote Originally Posted by Zlatomir View Post
    Also the compare function/functor should be declared before calling qSort
    Qt Code:
    1. bool compareWordItem ( const WordItem &w1 , const WordItem &w2 )
    2. {
    3. return w1.count > w2.count;
    4. }
    5. qStableSort(list.begin(),list.end(),compareWordItem);
    To copy to clipboard, switch view to plain text mode 

    LE: you declare/define compareWordItem outside of any function (including main)
    I already did that .

  12. #9
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: how to sort a list by one of its items ?!

    As Zlatomir says

    This is your clue:
    Qt Code:
    1. <unresolved overloaded function type>
    To copy to clipboard, switch view to plain text mode 

  13. #10
    Join Date
    Jan 2010
    Posts
    18
    Thanks
    11
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: how to sort a list by one of its items ?!

    Quote Originally Posted by tbscope View Post
    As Zlatomir says

    This is your clue:
    Qt Code:
    1. <unresolved overloaded function type>
    To copy to clipboard, switch view to plain text mode 
    if you mean defining it in my header file , yeah I defined it in my .h file

  14. #11
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: how to sort a list by one of its items ?!

    No, you should define that in the .cpp file, don't define functions in the .h file, you might get multiple-definitions errors if you include the header in many .cpp files.
    In the header files you should put only declarations, not definitions...

    I made a simple example, the code works, you should play with where you put the declarations/definitions, this is the little test i made:
    Qt Code:
    1. #include <QtCore>
    2. #include <iostream>
    3.  
    4. struct WordItem {
    5. WordItem(int i) : count(i) {}
    6. int count;
    7. };
    8.  
    9. bool compareWordItem ( const WordItem &w1 , const WordItem &w2 )
    10. {
    11. return w1.count > w2.count;
    12. }
    13.  
    14. int main(int argc, char *argv[])
    15. {
    16. QList<WordItem> list;
    17.  
    18. list.append(10);
    19. list.append(11);
    20. list.append(1);
    21.  
    22. qStableSort(list.begin(),list.end(),compareWordItem);
    23.  
    24. for(QList<WordItem>::iterator i = list.begin(); i!= list.end(); ++i)
    25. qDebug() << (*i).count;
    26.  
    27. std::cin.get();
    28. return 0;
    29. }
    To copy to clipboard, switch view to plain text mode 

  15. The following user says thank you to Zlatomir for this useful post:

    gbmtoday (2nd January 2011)

  16. #12
    Join Date
    Jan 2010
    Posts
    18
    Thanks
    11
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: how to sort a list by one of its items ?!

    No I meant I declared it in my .h and defined it .cpp ( sorry ! bad English )

    I don't get it , my code looks exactly as yours except that mine is in a class .

  17. #13
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: how to sort a list by one of its items ?!

    Quote Originally Posted by gbmtoday View Post
    except that mine is in a class .
    Don't forget the namespace!

  18. #14
    Join Date
    Jan 2010
    Posts
    18
    Thanks
    11
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: how to sort a list by one of its items ?!

    finally I figured what is the problem .

    I should have defined it as a static function.
    in .h :
    Qt Code:
    1. static bool compareWordItem ( const WordItem &w1 , const WordItem &w2 );
    To copy to clipboard, switch view to plain text mode 

    thanks guys

Similar Threads

  1. Replies: 1
    Last Post: 29th November 2010, 18:22
  2. Comparing Items In List Box
    By kenny_isles in forum Qt Programming
    Replies: 9
    Last Post: 21st February 2007, 14:06
  3. Items in QListView should sort on Header Click
    By vinnu in forum Qt Programming
    Replies: 14
    Last Post: 10th November 2006, 13:49
  4. delete items from list box
    By vvbkumar in forum Qt Programming
    Replies: 4
    Last Post: 23rd June 2006, 20:08
  5. Drag and drop items in view to sort order
    By Big Duck in forum Qt Programming
    Replies: 1
    Last Post: 25th May 2006, 20:43

Tags for this Thread

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.