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:
struct Position {
enum { Student, Employee } Type;
Type list;
int index;
Position(Type l, int i) { list = l; index = i; }
Position() { l = Student; index = -1;}
};
struct Position {
enum { Student, Employee } Type;
Type list;
int index;
Position(Type l, int i) { list = l; index = i; }
Position() { l = Student; index = -1;}
};
To copy to clipboard, switch view to plain text mode
then fill that list using two source lists:
QList<Position> positions;
for(int i=0;i<students.size();++i) {
positions << Position(Position::Student, i);
}
for(int i=0;i<employees.size();++i) {
positions << Position(Position::Employee, i);
}
QList<Position> positions;
for(int i=0;i<students.size();++i) {
positions << Position(Position::Student, i);
}
for(int i=0;i<employees.size();++i) {
positions << Position(Position::Employee, i);
}
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:
class SortFunctor {
public:
SortFunctor(const QList<Student> &students, const QList<Employee> &employees) : m_students(students), m_employees(employees) {}
bool operator()(const Position &p1, const Position &p2) const {
QDateTime dt1
= (p1.
list == Student
) ? m_students.
at(p1.
index).
time : m_employees.
at(p1.
index).
time;
QDateTime dt2
= (p2.
list == Student
) ? m_students.
at(p2.
index).
time : m_employees.
at(p2.
index).
time;
return dt1 < dt2;
}
};
class SortFunctor {
public:
SortFunctor(const QList<Student> &students, const QList<Employee> &employees) : m_students(students), m_employees(employees) {}
bool operator()(const Position &p1, const Position &p2) const {
QDateTime dt1 = (p1.list == Student) ? m_students.at(p1.index).time : m_employees.at(p1.index). time;
QDateTime dt2 = (p2.list == Student) ? m_students.at(p2.index).time : m_employees.at(p2.index). time;
return dt1 < dt2;
}
};
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:
qSort(positions.begin(), positions.end(), SortFunctor(students, employees));
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.
Bookmarks