Results 1 to 11 of 11

Thread: Comparing two QList with date time as parameter

  1. #1
    Join Date
    Sep 2013
    Posts
    40
    Thanks
    6
    Thanked 3 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Post Comparing two QList with date time as parameter

    Hi All,
    Is there any way for sorting two QList of different objects.
    for e.g. I have two POCO(Plain Old Cpp objects) Classes (Student & Employee), & both have CREATION_TIME field in db (consists when the record is created).
    Now I have to sort this both QList (QList<Student> & QList<Employee>) by their CREATION_TIME.
    Can it be done programmatically? or can it be achieved by some SQL query?

    Any kind of help will be appreciated.

    Thanks in Advance.

    PS : CREATION_TIME is of type DATETIME (QDateTime).

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

    Default Re: Comparing two QList with date time as parameter

    What do you mean by "two lists of different objects"? Do you wish to get one list as a result or two separate lists, one for each source list?
    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.


  3. #3
    Join Date
    Sep 2013
    Posts
    40
    Thanks
    6
    Thanked 3 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Post Re: Comparing two QList with date time as parameter

    Quote Originally Posted by wysota View Post
    What do you mean by "two lists of different objects"? Do you wish to get one list as a result or two separate lists, one for each source list?
    It is two different lists.(To be compared by specific value i.e. CREATION_TIME)
    e.g. QList<Student> & QList<Employee>.

    PS : And I think the result will be stored in the other list after comparison.
    for e.g.:
    Qt Code:
    1. QList<QObject> listNew;
    2. QList<Employee> listEmp;
    3. QList<Student> listStud;
    4.  
    5. for(i=0;;i++)
    6. if(listEmp.at(i).creTime<listStud.at(i).creTime)
    7. listNew.add(listEmp.at(i));
    8. else
    9. listNew.add(listStud.at(i));
    To copy to clipboard, switch view to plain text mode 

    Pls ignore the mistakes as I am new to this technology & know very less about this.

    Thanks.
    Last edited by rohitkk; 9th June 2014 at 11:23.

  4. #4
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,521
    Thanked 278 Times in 273 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Comparing two QList with date time as parameter

    Read about qSort function.

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

    Default Re: Comparing two QList with date time as parameter

    Quote Originally Posted by rohitkk View Post
    It is two different lists.(To be compared by specific value i.e. CREATION_TIME)
    e.g. QList<Student> & QList<Employee>.

    PS : And I think the result will be stored in the other list after comparison.
    for e.g.:
    Qt Code:
    1. QList<QObject> listNew;
    2. QList<Employee> listEmp;
    3. QList<Student> listStud;
    4.  
    5. for(i=0;;i++)
    6. if(listEmp.at(i).creTime<listStud.at(i).creTime)
    7. listNew.add(listEmp.at(i));
    8. else
    9. listNew.add(listStud.at(i));
    To copy to clipboard, switch view to plain text mode 

    Pls ignore the mistakes as I am new to this technology & know very less about this.

    Thanks.
    So... you have a list of type A, a list of type B and you want as a result a list of type C?

    Do A and B have a common base class?
    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.


  6. #6
    Join Date
    Sep 2013
    Posts
    40
    Thanks
    6
    Thanked 3 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Comparing two QList with date time as parameter

    Quote Originally Posted by Lesiok View Post
    Read about qSort function.
    I think qSort can be used to sort single list or list of Single data type.

    Quote Originally Posted by wysota View Post
    So... you have a list of type A, a list of type B and you want as a result a list of type C?
    Yes.As I cannot have list of type
    Qt Code:
    1. QList<Student,Employee>
    To copy to clipboard, switch view to plain text mode 
    , I have to declare a
    Qt Code:
    1. QList<QObject>
    To copy to clipboard, switch view to plain text mode 
    which will have the data for both 'Student' & 'Employee'.

    Do A and B have a common base class?
    No.

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

    Default Re: Comparing two QList with date time as parameter

    Do Student and Employee inherit QObject? That's very unlikely as QObject cannot be copied thus you cannot have a QList<QObject>, only QList<QObject*>.

    If classes A and B do not have a common base class C then I suggest to create a structure marking indexes in the source lists:

    Qt Code:
    1. struct Position {
    2. enum { Student, Employee } Type;
    3. Type list;
    4. int index;
    5. Position(Type l, int i) { list = l; index = i; }
    6. Position() { l = Student; index = -1;}
    7. };
    To copy to clipboard, switch view to plain text mode 

    then fill that list using two source lists:

    Qt Code:
    1. QList<Position> positions;
    2. for(int i=0;i<students.size();++i) {
    3. positions << Position(Position::Student, i);
    4. }
    5.  
    6. for(int i=0;i<employees.size();++i) {
    7. positions << Position(Position::Employee, i);
    8. }
    To copy to clipboard, switch view to plain text mode 

    And then sort that list using qSort (or std::sort) and a custom sorting function object that accepts the two source functions as parameters and then sorts the position list according to those two lists:

    Qt Code:
    1. class SortFunctor {
    2. public:
    3. SortFunctor(const QList<Student> &students, const QList<Employee> &employees) : m_students(students), m_employees(employees) {}
    4. bool operator()(const Position &p1, const Position &p2) const {
    5. QDateTime dt1 = (p1.list == Student) ? m_students.at(p1.index).time : m_employees.at(p1.index). time;
    6. QDateTime dt2 = (p2.list == Student) ? m_students.at(p2.index).time : m_employees.at(p2.index). time;
    7. return dt1 < dt2;
    8. }
    9. };
    To copy to clipboard, switch view to plain text mode 

    In the end you'll get a list of Position objects sorted by time in the source lists:
    Qt Code:
    1. qSort(positions.begin(), positions.end(), SortFunctor(students, employees));
    To copy to clipboard, switch view to plain text mode 

    Then you can use that list as indirect reference to the other two lists (like in the sorting functor).

    Note: If original lists are already sorted by time then there is a simpler solution -- perform a merging step of the merge sort algorithm (i.e. iteratate over the two lists choosing from a list as long as the time is smaller than that of the current item in the other list) building the position list in O(m+n) time.
    Last edited by wysota; 9th June 2014 at 12:15.
    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.


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

    anda_skoa (9th June 2014)

  9. #8
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,419
    Thanks
    37
    Thanked 1,546 Times in 1,496 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Comparing two QList with date time as parameter

    Quote Originally Posted by rohitkk View Post
    Now I have to sort this both QList (QList<Student> & QList<Employee>) by their CREATION_TIME.
    qSort() has an overload that allows you to specify a "less than" predicate, e.g. simple function returning bool and taking two of the objects

    Qt Code:
    1. bool studentLessThanByCreationTime(const Student &s1, const Student &s2)
    2. {
    3. return s1.creationtime < s2.creationtime;
    4. }
    To copy to clipboard, switch view to plain text mode 


    Quote Originally Posted by rohitkk View Post
    or can it be achieved by some SQL query?
    Very likely, there is an "ORDER BY" keyword in SQL.

    Cheers,
    _

  10. #9
    Join Date
    Sep 2013
    Posts
    40
    Thanks
    6
    Thanked 3 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Comparing two QList with date time as parameter

    Hi All,
    I think my question is misunderstood.
    I can get the list of 'Student' & 'Employee' sorted by 'CREATION_TIME' by simply using "ORDER BY CREATION_TIME" in the end of sql query statement.
    Hence, getting individual list sorted is not a challenge but sorting both the list of 'Student' & 'Employee' & merging into a different list is challenge.
    I think now my question is more clear.

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

    Default Re: Comparing two QList with date time as parameter

    If you tell me how to merge a list of bananas and a list of airplanes into one list of shoes then I will tell you how to merge a list of students and a list of employees into a list of whatever else you can imagine. Without that the solution I gave you with a list of references to the other two lists is the closest you can get unless you make students and employees share a common base class exposing the creation time as one of its public functions.

    In SQL you can select from an union of two tables, getting the merged list sorted by whatever you want that is shared between the two tables (e.g. creation time).
    Last edited by wysota; 9th June 2014 at 13:19.
    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. #11
    Join Date
    Sep 2013
    Posts
    40
    Thanks
    6
    Thanked 3 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Smile Re: Comparing two QList with date time as parameter

    I have achieved this by using UNION,
    Below is my query,

    " SELECT CREATION_TIME,COL2,COL3 FROM 'student_info_table' UNION ALL SELECT CREATION_TIME,COL2,COL3 FROM 'employee_info_table' ORDER BY CREATION_TIME ASC"

    Thank you all ,
    for giving time & taking Interest.

Similar Threads

  1. Replies: 0
    Last Post: 5th October 2012, 12:17
  2. Date Time
    By javed_alam786 in forum Qt Programming
    Replies: 2
    Last Post: 11th May 2011, 15:52
  3. other time...can't get date
    By mmm286 in forum Newbie
    Replies: 7
    Last Post: 4th March 2010, 16:53
  4. Date Time
    By starcontrol in forum Qt Programming
    Replies: 3
    Last Post: 4th April 2008, 12:02

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.