Results 1 to 14 of 14

Thread: Sorting using qSort(), - if QList contains POINTERS

  1. #1
    Join Date
    Feb 2006
    Posts
    157
    Thanks
    12
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4

    Default Sorting using qSort(), - if QList contains POINTERS

    Hi guys,


    How to use qSort() if the QList contains pointers like this...
    Qt Code:
    1. QList < double* > _doubleList;
    2. // this is for simple easyness , you can give QList<employee*>
    3.  
    4. _doubleList .append( new( 2.22) );
    5. _doubleList .append( new( 1.22) );
    6. _doubleList .append( new( 4.22) );
    7. _doubleList .append( new( 3.22) );
    8.  
    9. qSort( _doubleList ); // This code will not sort the data properly
    To copy to clipboard, switch view to plain text mode 

    while using the above "_doubleList" the data is not sorting .
    But if am using the list like this ...

    Qt Code:
    1. QList < double > _doubleList;
    2. // this is for simple easyness , you can give QList<employee*>
    3.  
    4. _doubleList .append( 2.22 );
    5. _doubleList .append( 1.22 );
    6. _doubleList .append( 4.22 );
    7. _doubleList .append( 3.22 );
    8.  
    9. qSort( _doubleList ); // This code WILL SORT the data properly
    To copy to clipboard, switch view to plain text mode 


    please tell me how to sort a QList which contains pointers.
    thanks in advance

  2. #2
    Join Date
    Jan 2006
    Location
    Bremen, Germany
    Posts
    554
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Sorting using qSort(), - if QList contains POINTERS

    You have to implement your own lessThan operator like shown here:

    http://doc.trolltech.com/4.3/qtalgorithms.html#qSort-2

  3. #3
    Join Date
    Feb 2006
    Posts
    157
    Thanks
    12
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4

    Default Re: Sorting using qSort(), - if QList contains POINTERS

    can anybody help me to sort the following code.
    My intention is to sort the sudent object in assending order( based on the _rollNo ).
    Qt Code:
    1. class Student
    2. {
    3. public:
    4. Student()
    5. { cout<<"\n" << __FUNCTION__; }
    6. Student(int roolNo ):_rolNo( roolNo )
    7. { cout<<"\n" << __FUNCTION__; }
    8. ~Student()
    9. { cout<<"\n" << __FUNCTION__; }
    10. bool toAssending( Student & s1 , Student & s2 )
    11. {
    12. cout<<"\n" << __FUNCTION__;
    13. return s1.getRollNo() < s2.getRollNo();
    14. }
    15. int getRollNo()
    16. {
    17. return _rolNo;
    18. }
    19. private :
    20. int _rolNo;
    21.  
    22. };
    23.  
    24. int main()
    25. {
    26.  
    27. QList<Student*> studentList ;
    28. studentList.append( new Student(2) );
    29. studentList.append( new Student(4) );
    30. studentList.append( new Student(3) );
    31. studentList.append( new Student(1) );
    32. studentList.append( new Student(5) );
    33. QListIterator<Student*> it( studentList );
    34. Student* val = 0;
    35.  
    36. cout<<"\n ****** Before SORT ***** " ;
    37. while( it.hasNext() )
    38. {
    39. val = it.next();
    40. cout<<"\n" << val->getRollNo();
    41.  
    42. }
    43.  
    44. qSort( studentList.begin() , studentList.end(), toAssending ); // please correct syntax if am wrong
    45.  
    46. cout<<"\n ****** After SORT ***** " ;
    47. QListIterator<Student*> it_1( studentList );
    48. Student* val_1 = 0;
    49. while( it_1.hasNext() )
    50. {
    51. val_1 = it_1.next();
    52. cout<<"\n" << val_1->getRollNo();
    53. }
    54.  
    55. qDeleteAll( studentList );
    56.  
    57.  
    58.  
    59. return 0;
    60. }
    To copy to clipboard, switch view to plain text mode 

    thanks

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

    Default Re: Sorting using qSort(), - if QList contains POINTERS

    Make toAssending() a non-member function.
    Qt Code:
    1. class Student
    2. {
    3. ...
    4. };
    5.  
    6. bool toAssending( const Student & s1 , const Student & s2 )
    7. {
    8. cout<<"\n" << __FUNCTION__;
    9. return s1.getRollNo() < s2.getRollNo();
    10. }
    11.  
    12. int main()
    13. {
    14. ...
    15. qSort( studentList.begin() , studentList.end(), toAssending );
    16. ...
    17. }
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  5. #5
    Join Date
    Feb 2006
    Posts
    157
    Thanks
    12
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4

    Angry Re: Sorting using qSort(), - if QList contains POINTERS

    jpn,

    I have tried the same code its not working. i am getting compilation error.
    here is my code
    Qt Code:
    1. class Student
    2. {
    3. public:
    4. Student()
    5. { cout<<"\n" << __FUNCTION__; }
    6. Student(int roolNo ):_rolNo( roolNo )
    7. { cout<<"\n" << __FUNCTION__; }
    8. ~Student()
    9. { cout<<"\n" << __FUNCTION__; }
    10. /*
    11. bool toAssending( Student & s1 , Student & s2 )
    12. {
    13. cout<<"\n" << __FUNCTION__;
    14. return s1.getRollNo() < s2.getRollNo();
    15. }*/
    16. int getRollNo() const
    17. {
    18. return _rolNo;
    19. }
    20. private :
    21. int _rolNo;
    22.  
    23. };
    24.  
    25. bool toAssending( const Student & s1 , const Student & s2 )
    26. {
    27. cout<<"\n" << __FUNCTION__;
    28. return s1.getRollNo() < s2.getRollNo();
    29. }
    30.  
    31. int main()
    32. {
    33. QList<Student*> studentList ;
    34. studentList.append( new Student(2) );
    35. studentList.append( new Student(4) );
    36. studentList.append( new Student(3) );
    37. studentList.append( new Student(1) );
    38. studentList.append( new Student(5) );
    39. QListIterator<Student*> it( studentList );
    40. Student* val = 0;
    41. cout<<"\n ****** Before SORT ***** " ;
    42. while( it.hasNext() )
    43. {
    44. val = it.next();
    45. //cout<<"\n" << val;
    46. cout<<"\n" << val->getRollNo();
    47.  
    48. }
    49.  
    50. qSort( studentList.begin() , studentList.end(), toAssending ); //here am using the global function. instead of class -member function.
    51.  
    52. cout<<"\n ****** After SORT ***** " ;
    53. QListIterator<Student*> it_1( studentList );
    54. Student* val_1 = 0;
    55. while( it_1.hasNext() )
    56. {
    57. val_1 = it_1.next();
    58. cout<<"\n" << val_1;
    59. cout<<"\n" << val_1->getRollNo();
    60. }
    61.  
    62. qDeleteAll( studentList );
    63.  
    64. return 0;
    65. }
    To copy to clipboard, switch view to plain text mode 

    here is the compliation error.
    please help me to resolve it.
    Qt Code:
    1. main.cpp(311) : warning C4101: 'assending' : unreferenced local variable
    2. c:\Qt\4.2.2\include\QtCore\../../src\corelib\tools\qalgorithms.h(355) : error C2664: 'bool (const Student &,const Student &)' : cannot convert parameter 2 from 'Student *' to 'const Student &'
    3. Reason: cannot convert from 'Student *' to 'const Student'
    4. No constructor could take the source type, or constructor overload resolution was ambiguous
    5. c:\Qt\4.2.2\include\QtCore\../../src\corelib\tools\qalgorithms.h(180) : see reference to function template instantiation 'void QAlgorithmsPrivate::qSortHelper<RandomAccessIterator,T,bool(__cdecl *)(const Student &,const Student &)>(RandomAccessIterator,RandomAccessIterator,const T & ,LessThan)' being compiled
    6. with
    7. [
    8. RandomAccessIterator=QList<Student *>::iterator,
    9. T=Student *,
    10. LessThan=bool (__cdecl *)(const Student &,const Student &)
    11. ]
    12. main.cpp(312) : see reference to function template instantiation 'void qSort<QList<T>::iterator,bool(__cdecl *)(const Student &,const Student &)>(RandomAccessIterator,RandomAccessIterator,LessThan)' being compiled
    13. with
    14. [
    15. T=Student *,
    16. RandomAccessIterator=QList<Student *>::iterator,
    17. LessThan=bool (__cdecl *)(const Student &,const Student &)
    18. ]
    19. c:\Qt\4.2.2\include\QtCore\../../src\corelib\tools\qalgorithms.h(355) : error C2664: 'bool (const Student &,const Student &)' : cannot convert parameter 1 from 'Student *' to 'const Student &'
    20. Reason: cannot convert from 'Student *' to 'const Student'
    21. No constructor could take the source type, or constructor overload resolution was ambiguous
    22. c:\Qt\4.2.2\include\QtCore\../../src\corelib\tools\qalgorithms.h(360) : error C2664: 'bool (const Student &,const Student &)' : cannot convert parameter 2 from 'Student *' to 'const Student &'
    23. Reason: cannot convert from 'Student *' to 'const Student'
    24. No constructor could take the source type, or constructor overload resolution was ambiguous
    25. c:\Qt\4.2.2\include\QtCore\../../src\corelib\tools\qalgorithms.h(360) : error C2664: 'bool (const Student &,const Student &)' : cannot convert parameter 1 from 'Student *' to 'const Student &'
    26. Reason: cannot convert from 'Student *' to 'const Student'
    27. No constructor could take the source type, or constructor overload resolution was ambiguous
    28. c:\Qt\4.2.2\include\QtCore\../../src\corelib\tools\qalgorithms.h(362) : error C2664: 'bool (const Student &,const Student &)' : cannot convert parameter 2 from 'Student *' to 'const Student &'
    29. Reason: cannot convert from 'Student *' to 'const Student'
    30. No constructor could take the source type, or constructor overload resolution was ambiguous
    31. c:\Qt\4.2.2\include\QtCore\../../src\corelib\tools\qalgorithms.h(362) : error C2664: 'bool (const Student &,const Student &)' : cannot convert parameter 1 from 'Student *' to 'const Student &'
    32. Reason: cannot convert from 'Student *' to 'const Student'
    33. No constructor could take the source type, or constructor overload resolution was ambiguous
    34. c:\Qt\4.2.2\include\QtCore\../../src\corelib\tools\qalgorithms.h(370) : error C2664: 'bool (const Student &,const Student &)' : cannot convert parameter 2 from 'Student *' to 'const Student &'
    35. Reason: cannot convert from 'Student *' to 'const Student'
    36. No constructor could take the source type, or constructor overload resolution was ambiguous
    37. c:\Qt\4.2.2\include\QtCore\../../src\corelib\tools\qalgorithms.h(370) : error C2664: 'bool (const Student &,const Student &)' : cannot convert parameter 1 from 'Student *' to 'const Student &'
    38. Reason: cannot convert from 'Student *' to 'const Student'
    39. No constructor could take the source type, or constructor overload resolution was ambiguous
    40. c:\Qt\4.2.2\include\QtCore\../../src\corelib\tools\qalgorithms.h(373) : error C2664: 'bool (const Student &,const Student &)' : cannot convert parameter 2 from 'Student *' to 'const Student &'
    41. Reason: cannot convert from 'Student *' to 'const Student'
    42. No constructor could take the source type, or constructor overload resolution was ambiguous
    43. c:\Qt\4.2.2\include\QtCore\../../src\corelib\tools\qalgorithms.h(373) : error C2664: 'bool (const Student &,const Student &)' : cannot convert parameter 1 from 'Student *' to 'const Student &'
    44. Reason: cannot convert from 'Student *' to 'const Student'
    45. No constructor could take the source type, or constructor overload resolution was ambiguous
    46. c:\Qt\4.2.2\include\QtCore\../../src\corelib\tools\qalgorithms.h(385) : error C2664: 'bool (const Student &,const Student &)' : cannot convert parameter 2 from 'Student *' to 'const Student &'
    47. Reason: cannot convert from 'Student *' to 'const Student'
    48. No constructor could take the source type, or constructor overload resolution was ambiguous
    49. c:\Qt\4.2.2\include\QtCore\../../src\corelib\tools\qalgorithms.h(385) : error C2664: 'bool (const Student &,const Student &)' : cannot convert parameter 1 from 'Student *' to 'const Student &'
    50. Reason: cannot convert from 'Student *' to 'const Student'
    51. No constructor could take the source type, or constructor overload resolution was ambiguous
    52.  
    53.  
    54. logFile - 12 error(s), 10 warning(s)
    To copy to clipboard, switch view to plain text mode 

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

    Default Re: Sorting using qSort(), - if QList contains POINTERS

    Right, the comparator function should naturally take pointers since that's what the container contains:
    Qt Code:
    1. bool toAssending( const Student * s1 , const Student * s2 )
    2. {
    3. cout<<"\n" << __FUNCTION__;
    4. return s1->getRollNo() < s2->getRollNo();
    5. }
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

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

    joseph (10th October 2007)

  8. #7
    Join Date
    Feb 2006
    Posts
    157
    Thanks
    12
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4

    Default Re: Sorting using qSort(), - if QList contains POINTERS

    could you please me , how this qSort() is working .
    Are we passing function pointer to qSort() ...??



    thanks

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

    Default Re: Sorting using qSort(), - if QList contains POINTERS

    Quote Originally Posted by joseph View Post
    could you please me , how this qSort() is working.
    You can always check Qt sources for implementation details. You can find qSort() implementation in src/corelib/tools/qalgorithms.h.
    Are we passing function pointer to qSort() ...??
    Yes, qSort() calls the passed function to compare items.
    J-P Nurmi

  10. #9
    Join Date
    Feb 2006
    Posts
    157
    Thanks
    12
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4

    Angry Re: Sorting using qSort(), - if QList contains POINTERS

    First of all thanks jpn,
    But please see the below code for compare <double*>
    Qt Code:
    1. bool putDoubleListInAscendingOrder( const double * d1 , const double * d2 )
    2. {
    3. cout<<"\n" << __FUNCTION__;
    4. return *d1 < *d2;
    5. }
    6.  
    7. int main()
    8. {
    9.  
    10. QList<double*> _doubleList ;
    11. _doubleList.append( new double(5.12) );
    12. _doubleList.append( new double(2.22) );
    13. _doubleList.append( new double(3.22) );
    14. _doubleList.append( new double(1.22) );
    15. _doubleList.append( new double(4.22) );
    16.  
    17.  
    18. QListIterator<double*> it( _doubleList );
    19. double* d = 0;
    20. cout<<"\n ****** Before SORT ***** " ;
    21. while( it.hasNext() )
    22. {
    23. d = it.next();
    24. cout<<"\n" << *d;
    25. }
    26. cout<<"\n ****** After SORT ***** " ;
    27. qSort( _doubleList.begin(), _doubleList.end() , putDoubleListInAscendingOrder );
    28. it. toFront(); // Note tat i am using the same iterator
    29. // According to assistant , iterator will move before 1-st item
    30. // if i am calling it.next(), then it is sapposed to return the first item in
    31. // _doubleList .
    32.  
    33. d= it.next();
    34. cout<<"\n d :" << *d ; // Here the expected result is "1.22"
    35.  
    36. // N:B => But am getting the reuslt of cout is = "5.12" ...why..????
    37.  
    38.  
    39. return 0;
    40. }
    To copy to clipboard, switch view to plain text mode 

    but if am changing the code as below. ( with different iterator or _doubleList.first() will return the expected result ie; "1.22" )

    Qt Code:
    1. int main()
    2. {
    3.  
    4. QList<double*> _doubleList ;
    5. _doubleList.append( new double(5.12) );
    6. _doubleList.append( new double(2.22) );
    7. _doubleList.append( new double(3.22) );
    8. _doubleList.append( new double(1.22) );
    9. _doubleList.append( new double(4.22) );
    10.  
    11.  
    12. QListIterator<double*> it( _doubleList );
    13. double* d = 0;
    14. cout<<"\n ****** Before SORT ***** " ;
    15. while( it.hasNext() )
    16. {
    17. d = it.next();
    18. cout<<"\n" << *d;
    19. }
    20. cout<<"\n ****** After SORT ***** " ;
    21. qSort( _doubleList.begin(), _doubleList.end() , putDoubleListInAscendingOrder );
    22.  
    23. QListIterator<double*> it_2 ( _doubleList )
    24. it_2. next() ;
    25. d= it_2. next();
    26. cout<<"\n d :" << *d ; // Here the result is "1.22"
    27.  
    28. // could you please explain why ...?? or If my understanding is wrong please correct me..
    29.  
    30. return 0;
    31. }
    To copy to clipboard, switch view to plain text mode 

  11. #10
    Join Date
    Jan 2006
    Location
    Bremen, Germany
    Posts
    554
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Sorting using qSort(), - if QList contains POINTERS

    'it' is invalid after qSort()

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

    Default Re: Sorting using qSort(), - if QList contains POINTERS

    Indeed, as QListIterator docs state:
    QListIterator<T> allows you to iterate over a QList<T> (or a QQueue<T>). If you want to modify the list as you iterate over it, use QMutableListIterator<T> instead.
    Also, may I ask what's the point of allocating doubles on the heap? Notice that QList is an implicitly shared class.
    J-P Nurmi

  13. #12
    Join Date
    Feb 2006
    Posts
    157
    Thanks
    12
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4

    Default Re: Sorting using qSort(), - if QList contains POINTERS

    Quote Originally Posted by jpn View Post
    Indeed, as QListIterator docs state:

    Also, may I ask what's the point of allocating doubles on the heap? Notice that QList is an implicitly shared class.

    Thanks for you information.
    yes you are right. There is no need for <double*>.
    But it was just an example.


    I have tried with QMutableListIterator . its working perfectly
    thanks

  14. #13
    Join Date
    Mar 2012
    Posts
    2
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Sorting using qSort(), - if QList contains POINTERS


  15. #14
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Sorting using qSort(), - if QList contains POINTERS

    I am sure that 6 years ago, your advice would have been useful. Meanwhile, the OP's original project was canceled, the company went under, and he's had 4 different jobs since then.

    Functors are nice, but when all you need is a simple static function that takes two pointers to double as arguments and returns a bool, adding all that overhead is overkill. And will the person who inherits this code take a look and ask, "Why the heck did they use functors when a static function would have worked just as well? Must be reading too many advanced C++ books".

    Been there, done that. Functors have their uses and many C++ generic programming template constructs could not be implemented without them. Sorting a list of pointers by the value of the pointed-to instance isn't one of them.
    Last edited by d_stranz; 18th August 2013 at 19:00.

Similar Threads

  1. QList of pointers
    By Farcaller in forum Newbie
    Replies: 4
    Last Post: 24th January 2006, 16:48

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.