Results 1 to 9 of 9

Thread: Sorting a series of QLists into the same order

  1. #1
    Join Date
    Dec 2010
    Posts
    31
    Thanks
    9
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Sorting a series of QLists into the same order

    Hi, i have a series of QLists (1,2,3,4 etc) with float values all in a random order. They are all of the same length. Now i then use:

    Qt Code:
    1. qSort(list1.begin(), list1.end());
    To copy to clipboard, switch view to plain text mode 

    to sort list1 into ascending order. OK but how can i sort all the other lists based on the order of this list? E.g. if list1[0] is moved to list1[10] during sorting i want the same thing to occur with all other lists no matter what their values.

    Thanks for your help

    Matt

  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: Sorting a series of QLists into the same order

    The simplest solution is to create a struct (class) with 4 float members, create the QList<Your_Class> and sort it depending on what member you need.
    But off course this might not "fit" your problem well, so provide more information on what you are trying to achieve.

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

    mobucl (23rd February 2011)

  4. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Sorting a series of QLists into the same order

    Create another list that will represent your items order (let's call it "order"). Have it contain elements from 0 to n-1 in sequence (where n is the size of your list1). Then sort that list according to the desired order of items in "list1" (you'll probably need to implement your own lessThan functor for this). You'll receive a list where order[i] represents an index of an element that should occupy position "i". Then loop through all your lists and rearrange their items according to the order of items from "order".


    Added after 22 minutes:


    Here is a sample implementation:
    Qt Code:
    1. #include <QtCore>
    2.  
    3. struct Elem {
    4. Elem(int p, int v) { pos = p; val = v; }
    5. int pos;
    6. int val;
    7. };
    8.  
    9. bool myLessThan(const Elem& e1, const Elem &e2){
    10. return (e1.val < e2.val);
    11. }
    12.  
    13. int main(int argc, char **argv) {
    14. QList<int> random;
    15. QList<Elem> order;
    16. QList<int> other;
    17. for(int i=0;i<10;++i){
    18. random.push_back(qrand()%100);
    19. order.push_back(Elem(i,random.at(i)));
    20. other.push_back(qrand()%100);
    21. }
    22. qStableSort(order.begin(), order.end(), myLessThan);
    23. QList<int> sorted;
    24. for(int i=0;i<other.size();++i){
    25. sorted.push_back(other.at(order.at(i).pos));
    26. }
    27. qDebug() << "R:" << random;
    28. qDebug() << "O:" << other;
    29. qDebug() << "S:" << sorted;
    30. }
    To copy to clipboard, switch view to plain text mode 

    Result:
    text Code:
    1. R: (13, 8, 77, 38, 91, 66, 69, 91, 61, 30)
    2. O: (7, 91, 99, 63, 92, 7, 8, 39, 43, 61)
    3. S: (91, 7, 61, 63, 43, 7, 8, 99, 92, 39)
    To copy to clipboard, switch view to plain text mode 
    Last edited by wysota; 22nd February 2011 at 12:07.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


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

    mobucl (23rd February 2011)

  6. #4
    Join Date
    Dec 2010
    Posts
    31
    Thanks
    9
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Sorting a series of QLists into the same order

    hi Zlatomir and wysota,

    Thanks for your input - i will take a look at this and get back to you if i need any more information!

    Many thanks

    Matt

  7. #5
    Join Date
    Dec 2010
    Posts
    31
    Thanks
    9
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Sorting a series of QLists into the same order

    Hi wysota,

    Ok so i can get your example to work fine so i now want to create a class based around this (please bear in mind im quite new to c++ and learning as i go so sorry if this is a simple thing i should know the answer too!) SO here is my order.h file:

    Qt Code:
    1. #ifndef ORDER_H
    2. #define ORDER_H
    3. #include<QList>
    4.  
    5. class Order
    6. {
    7. struct Elem {
    8. Elem(int p, float v) { pos = p; val = v; }
    9. int pos;
    10. float val;
    11. };
    12. public:
    13. QList<Elem> MainOrder;
    14. Order(QList<float>);
    15. QList<float> Sort(QList<float> ToSort);
    16. };
    17.  
    18. #endif // ORDER_H
    To copy to clipboard, switch view to plain text mode 

    and here is my order.cpp file:

    Qt Code:
    1. #include "order.h"
    2. #include <QtCore/QCoreApplication>
    3.  
    4. // initialise the class by providing a list which is then sorted
    5. Order::Order(QList<float> in)
    6. {
    7. for(int i=0;i<in.length();++i)
    8. {
    9. MainOrder.push_back(Elem(i,in.at(i)));
    10. }
    11. // qStableSort(MainOrder.begin(), MainOrder.end(), myLessThan);
    12. }
    13.  
    14.  
    15. //supply another list to the class which is to be sorted based on MainOrder
    16. QList<float> Order::Sort(QList<float> ToSort)
    17. {
    18. QList<float> SortedData;
    19. for(int i=0;i<MainOrder.size();++i)
    20. {
    21. SortedData.push_back(ToSort.at(MainOrder.at(i).pos));
    22. }
    23. return (SortedData);
    24. }
    To copy to clipboard, switch view to plain text mode 

    So the problem is that the code seems to work correctly when a new instance of the class is created and it appears that MainOrder fills with the desired data, however i cannot use the line

    Qt Code:
    1. qStableSort(MainOrder.begin(), MainOrder.end(), myLessThan);
    To copy to clipboard, switch view to plain text mode 

    as no matter where i put the bool myLessThan function you described below i get (various depending on location) errors.....so can you please explain where im supposed to put this function to get that line to work!!!!! Ive tried everyting i can think of!

    Thanks for your time,

    Matt

  8. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Sorting a series of QLists into the same order

    Put it in order.cpp somewhere before the first method that calls this function.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  9. The following user says thank you to wysota for this useful post:

    mobucl (25th February 2011)

  10. #7
    Join Date
    Dec 2010
    Posts
    31
    Thanks
    9
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Sorting a series of QLists into the same order

    Hi wysota, if i added the bool function after the includes in the cpp file i get:

    error: ISO C++ forbids declaration of 'Elem' with no type

    so even though i have the structure Elem defined in the order.h file i guessed i needed it here also. So i then added this above the bool function. now when i compile i dont get an error which is great BUT as soon as i add the:

    Qt Code:
    1. qStableSort(MainOrder.begin(), MainOrder.end(), myLessThan);
    To copy to clipboard, switch view to plain text mode 

    Line get loads of problems:

    In file included from ..\..\..\qt\include/QtCore/qalgorithms.h:1,
    from ..\..\..\qt\include\QtCore/../../src/corelib/tools/qlist.h:47,
    from ..\..\..\qt\include\QtCore/qlist.h:1,
    from ..\..\..\qt\include\QtCore/QList:1,
    from ..\attempt1\/order.h:3,
    from ..\attempt1\order.cpp:1:

    ..\..\..\qt\include/QtCore/../../src/corelib/tools/qalgorithms.h: In function 'void QAlgorithmsPrivate::qMerge(RandomAccessIterator, RandomAccessIterator, RandomAccessIterator, T&, LessThan) [with RandomAccessIterator = QList<Order::Elem>::iterator, T = const Order::Elem, LessThan = bool (*)(const Elem&, const Elem&)]':

    ..\..\..\qt\include/QtCore/../../src/corelib/tools/qalgorithms.h:459: instantiated from 'void QAlgorithmsPrivate::qStableSortHelper(RandomAccess Iterator, RandomAccessIterator, const T&, LessThan) [with RandomAccessIterator = QList<Order::Elem>::iterator, T = Order::Elem, LessThan = bool (*)(const Elem&, const Elem&)]'

    ..\..\..\qt\include/QtCore/../../src/corelib/tools/qalgorithms.h:212: instantiated from 'void qStableSort(RandomAccessIterator, RandomAccessIterator, LessThan) [with RandomAccessIterator = QList<Order::Elem>::iterator, LessThan = bool (*)(const Elem&, const Elem&)]'

    ..\attempt1\order.cpp:21: instantiated from here

    ..\..\..\qt\include/QtCore/../../src/corelib/tools/qalgorithms.h:424: error: invalid initialization of reference of type 'const Elem&' from expression of type 'Order::Elem'

    ..\..\..\qt\include/QtCore/../../src/corelib/tools/qalgorithms.h: In function 'RandomAccessIterator QAlgorithmsPrivate::qLowerBoundHelper(RandomAccess Iterator, RandomAccessIterator, const T&, LessThan) [with RandomAccessIterator = QList<Order::Elem>::iterator, T = Order::Elem, LessThan = bool (*)(const Elem&, const Elem&)]':

    ..\..\..\qt\include/QtCore/../../src/corelib/tools/qalgorithms.h:252: instantiated from 'RandomAccessIterator qLowerBound(RandomAccessIterator, RandomAccessIterator, const T&, LessThan) [with RandomAccessIterator = QList<Order::Elem>::iterator, T = Order::Elem, LessThan = bool (*)(const Elem&, const Elem&)]'

    ..\..\..\qt\include/QtCore/../../src/corelib/tools/qalgorithms.h:435: instantiated from 'void QAlgorithmsPrivate::qMerge(RandomAccessIterator, RandomAccessIterator, RandomAccessIterator, T&, LessThan) [with RandomAccessIterator = QList<Order::Elem>::iterator, T = const Order::Elem, LessThan = bool (*)(const Elem&, const Elem&)]'

    ..\..\..\qt\include/QtCore/../../src/corelib/tools/qalgorithms.h:459: instantiated from 'void QAlgorithmsPrivate::qStableSortHelper(RandomAccess Iterator, RandomAccessIterator, const T&, LessThan) [with RandomAccessIterator = QList<Order::Elem>::iterator, T = Order::Elem, LessThan = bool (*)(const Elem&, const Elem&)]'

    ..\..\..\qt\include/QtCore/../../src/corelib/tools/qalgorithms.h:212: instantiated from 'void qStableSort(RandomAccessIterator, RandomAccessIterator, LessThan) [with RandomAccessIterator = QList<Order::Elem>::iterator, LessThan = bool (*)(const Elem&, const Elem&)]'

    ..\attempt1\order.cpp:21: instantiated from here

    ..\..\..\qt\include/QtCore/../../src/corelib/tools/qalgorithms.h:478: error: invalid initialization of reference of type 'const Elem&' from expression of type 'Order::Elem'

    ..\..\..\qt\include/QtCore/../../src/corelib/tools/qalgorithms.h: In function 'RandomAccessIterator QAlgorithmsPrivate::qUpperBoundHelper(RandomAccess Iterator, RandomAccessIterator, const T&, LessThan) [with RandomAccessIterator = QList<Order::Elem>::iterator, T = Order::Elem, LessThan = bool (*)(const Elem&, const Elem&)]':

    ..\..\..\qt\include/QtCore/../../src/corelib/tools/qalgorithms.h:285: instantiated from 'RandomAccessIterator qUpperBound(RandomAccessIterator, RandomAccessIterator, const T&, LessThan) [with RandomAccessIterator = QList<Order::Elem>::iterator, T = Order::Elem, LessThan = bool (*)(const Elem&, const Elem&)]'

    ..\..\..\qt\include/QtCore/../../src/corelib/tools/qalgorithms.h:440: instantiated from 'void QAlgorithmsPrivate::qMerge(RandomAccessIterator, RandomAccessIterator, RandomAccessIterator, T&, LessThan) [with RandomAccessIterator = QList<Order::Elem>::iterator, T = const Order::Elem, LessThan = bool (*)(const Elem&, const Elem&)]'

    ..\..\..\qt\include/QtCore/../../src/corelib/tools/qalgorithms.h:459: instantiated from 'void QAlgorithmsPrivate::qStableSortHelper(RandomAccess Iterator, RandomAccessIterator, const T&, LessThan) [with RandomAccessIterator = QList<Order::Elem>::iterator, T = Order::Elem, LessThan = bool (*)(const Elem&, const Elem&)]'

    ..\..\..\qt\include/QtCore/../../src/corelib/tools/qalgorithms.h:212: instantiated from 'void qStableSort(RandomAccessIterator, RandomAccessIterator, LessThan) [with RandomAccessIterator = QList<Order::Elem>::iterator, LessThan = bool (*)(const Elem&, const Elem&)]'

    ..\attempt1\order.cpp:21: instantiated from here

    ..\..\..\qt\include/QtCore/../../src/corelib/tools/qalgorithms.h:499: error: invalid initialization of reference of type 'const Elem&' from expression of type 'const Order::Elem'

    mingw32-make[1]: *** [debug/order.o] Error 1
    mingw32-make: *** [debug] Error 2

    The .h file is the same as described previously and the new cpp file looks like this now:

    Qt Code:
    1. #include "order.h"
    2. #include <QtCore/QCoreApplication>
    3.  
    4. struct Elem {
    5. Elem(int p, float v) { pos = p; val = v; }
    6. int pos;
    7. float val;
    8. };
    9.  
    10. bool myLessThan(const Elem& e1, const Elem &e2){
    11. return (e1.val < e2.val);
    12. }
    13.  
    14. // initialise the class by providing a list which is then sorted
    15. Order::Order(QList<float> in)
    16. {
    17. for(int i=0;i<in.length();++i)
    18. {
    19. MainOrder.push_back(Elem(i,in.at(i)));
    20. }
    21. qStableSort(MainOrder.begin(), MainOrder.end(), myLessThan);
    22. }
    23.  
    24.  
    25. //supply another list to the class which is to be sorted based on MainOrder
    26. QList<float> Order::Sort(QList<float> ToSort)
    27. {
    28. QList<float> SortedData;
    29. for(int i=0;i<MainOrder.size();++i)
    30. {
    31. SortedData.push_back(ToSort.at(MainOrder.at(i).pos));
    32. }
    33. return (SortedData);
    34. }
    To copy to clipboard, switch view to plain text mode 

    Is there anything else you can suggest. Thanks alot for your continued help!

  11. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Sorting a series of QLists into the same order

    You don't have a struct Elem. You have a struct Order::Elem, that's two completely different things.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


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

    mobucl (25th February 2011)

  13. #9
    Join Date
    Dec 2010
    Posts
    31
    Thanks
    9
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Sorting a series of QLists into the same order

    Ok now i see the problem! Thanks for all your help, the code is working as expected now!

Similar Threads

  1. QList of QLists
    By m15ch4 in forum Newbie
    Replies: 12
    Last Post: 16th October 2013, 22:36
  2. Renaming multidimensional QLists
    By mobucl in forum Newbie
    Replies: 3
    Last Post: 17th December 2010, 09:04
  3. drawing series of rotated ellipses
    By qtn00b in forum Newbie
    Replies: 1
    Last Post: 17th December 2009, 01:51
  4. help using QLists as data in QwtPlotCurve
    By esorensen in forum Qwt
    Replies: 1
    Last Post: 11th July 2008, 19:52
  5. QList of Qlists?
    By markcole in forum Qt Programming
    Replies: 6
    Last Post: 8th July 2008, 05:44

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.