qSort sorts the pointers, not the objects that these pointers point to. If you want to sort LogEvents with qSort, you will have to store them by value, not by reference (and also have comparison operators that take a link, qSort will not find your comparison functions with a pointer) or pass a third argument with the function you define.
This may explain why this is so with the examples.
LogEvent event1, event2; LogEvent *eventptr1=&event1,*eventptr2=&event2; event1<event2;
ETA: In the interest of having one complete answer to accept, I am going to rip off some good bits from the other answers here.
First, define the standard syntax less than the operator:
class LogEvent : public QTreeWidgetItem { public:
LogEvent.cpp
bool LogEvent::operator<(const LogEvent &event) {return timestamp<event.timestamp;}
Once this is done, you can use this template to dereference and compare leemes answers:
template<class T> bool dereferencedLessThan(T * o1, T * o2) { return *o1 < *o2; }
to sort your list as follows:
QVector<LogEvent *> currentItems; //add a bunch of LogEvent objects to currentItems qSort(list.begin(), list.end(), dereferencedLessThan<LogEvent>);
For completeness, it would be a good form to define standard syntax comparison operators for all your comparisons. Regardless of whether you support your custom comparison operators, you yourself.