Results 1 to 13 of 13

Thread: Don't understand qSort behaviour....

  1. #1
    Join Date
    Jul 2012
    Posts
    40
    Thanks
    12
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Don't understand qSort behaviour....

    Hi, i want to use qSort to Sort a QList but I have some problems:

    test.h
    Qt Code:
    1. class Test
    2. {
    3. public:
    4. int number;
    5. QString name;
    6.  
    7. public:
    8. Test(int number,QString name);
    9. QString toString();
    10.  
    11. bool operator< (const Test& t) const;
    12. };
    To copy to clipboard, switch view to plain text mode 
    test.cpp
    Qt Code:
    1. Test::Test(int number, QString name):
    2. number(number),
    3. name(name)
    4. {
    5. }
    6.  
    7. QString Test::toString()
    8. {
    9. return "Test( " + QString::number(number) + " , " + name + ")";
    10. }
    11.  
    12. bool Test::operator <(const Test& t) const
    13. {
    14. return this->number < t.number;
    15. }
    To copy to clipboard, switch view to plain text mode 

    main.cpp
    Qt Code:
    1. int main(int argc, char *argv[])
    2. {
    3. QCoreApplication a(argc, argv);
    4.  
    5. QList<Test*> list;
    6. list.append(new Test(10,"test1"));
    7. list.append(new Test(6,"test2"));
    8. list.append(new Test(8,"test3"));
    9. list.append(new Test(100,"test4"));
    10. list.append(new Test(1,"test5"));
    11. list.append(new Test(5,"test6"));
    12.  
    13. for(int i = 0; i < list.size(); i++){
    14. qDebug() << list.at(i)->toString();
    15. }
    16.  
    17. qSort(list.begin(),list.end());
    18. qDebug() << "sorted:";
    19.  
    20. for(int i = 0; i < list.size(); i++){
    21. qDebug() << list.at(i)->toString();
    22. }
    23.  
    24. return a.exec();
    25. }
    To copy to clipboard, switch view to plain text mode 

    Output:
    "Test( 6 , test2)"
    "Test( 8 , test3)"
    "Test( 100 , test4)"
    "Test( 1 , test5)"
    "Test( 5 , test6)"
    sorted:
    "Test( 100 , test4)"
    "Test( 10 , test1)"
    "Test( 6 , test2)"
    "Test( 8 , test3)"
    "Test( 1 , test5)"
    "Test( 5 , test6)"

  2. #2
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Don't understand qSort behaviour....

    because in this case pointers are compared, but not object itself.
    so, there are to possibility to fix this:
    1. populate a list with objects (not pointers)
    2. create LessThen function and pass it to qSort
    Qt Code:
    1. bool caseInsensitiveLessThan(const Test *s1, const Test *s2)
    2. {
    3. return *s1 < *s2;
    4. }
    5. ...
    6. qSort(list.begin(),list.end(), caseInsensitiveLessThan);
    7. ...
    To copy to clipboard, switch view to plain text mode 
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

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

    Qtonimo (2nd August 2012)

  4. #3
    Join Date
    Jul 2012
    Posts
    40
    Thanks
    12
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Don't understand qSort behaviour....

    So I don't use qSort, I use my own sort function:

    Qt Code:
    1. for( int size = list.size(); size > 1 ; size -= 1 )
    2. {
    3. for( int r = 1; r < size; r++ ) {
    4. int l = r - 1;
    5. if( list.at(l)->number > list.at(r)->number ) list.swap(l,r);
    6. }
    7. }
    To copy to clipboard, switch view to plain text mode 

  5. #4
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Don't understand qSort behaviour....

    why? why you're trying to reinvent the wheel? use second approach.
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  6. #5
    Join Date
    Jul 2012
    Posts
    40
    Thanks
    12
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Don't understand qSort behaviour....

    Because lessThan isn't a member of Test. The method has to be declared outside and is not encapsulated with my class Test

  7. #6
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Don't understand qSort behaviour....

    So, what? In this case you'll get only 3 lines of code + tested and optimized qSort function...
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  8. #7
    Join Date
    Jul 2012
    Posts
    40
    Thanks
    12
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Don't understand qSort behaviour....

    ok you persuaded me to use the second approach.
    It works very well....
    thank u

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

    Default Re: Don't understand qSort behaviour....

    @Qtonimo

    It makes sense for comparison functions involving Test to be associated with Test (either as a Test method as you did, or as an external friend function as is commonly seen) because the comparison function needs to know the internal structure of Test objects. The situation is completely different for spirit's comparison function for Test* which only uses Test's external interface (in the form of Test::operator<(const Test &)). So, no, there is no breach of encapsulation in spirit's suggestion, which I urge you to follow instead of rolling out your own inefficient sorting algorithm.

    Update: sorry, I missed your last reply.

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

    Qtonimo (2nd August 2012)

  11. #9
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Don't understand qSort behaviour....

    You are welcome.
    btw, you can make lessThan static function in Test class and then use it in qSort like this:
    Qt Code:
    1. class Test {
    2. ....
    3. public:
    4. static bool lessThan(Test *t1, Test *t2);
    5. ....
    6. };
    7. ...
    8. bool Test::lessThan(Test *t1, Test *t2)
    9. {
    10. return *t1 < *t2;
    11. }
    12. ...
    13. qSort(list.begin(),list.end(), &Test::lessThan);
    14. ...
    To copy to clipboard, switch view to plain text mode 
    Last edited by spirit; 2nd August 2012 at 16:02.
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  12. The following user says thank you to spirit for this useful post:

    Qtonimo (2nd August 2012)

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

    Default Re: Don't understand qSort behaviour....

    please, it is THAN, not then!
    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.

  14. #11
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Don't understand qSort behaviour....

    hm? if ... then? come on!
    then vs than.
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

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

    Default Re: Don't understand qSort behaviour....

    less THAN!
    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.

  16. #13
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Don't understand qSort behaviour....

    ok, note, next time use PM instead!
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

Similar Threads

  1. How to qSort a Qlist of QPair?
    By Trader in forum Qt Programming
    Replies: 2
    Last Post: 7th July 2010, 19:04
  2. QSort
    By skumar7777 in forum Qt Programming
    Replies: 4
    Last Post: 2nd December 2008, 06:18
  3. qSort with lessThan
    By soul_rebel in forum Qt Programming
    Replies: 4
    Last Post: 19th August 2008, 18:14
  4. qSort() problem
    By darksaga in forum Qt Programming
    Replies: 5
    Last Post: 17th August 2007, 11:47
  5. [Qt4] QSORT
    By jane in forum Qt Programming
    Replies: 6
    Last Post: 24th May 2006, 23:38

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.