Sorting dates in Qt
-
How do I utilize QDate to sort dates by year, month and day ?
-
welcome to devnet
QDate has all "relational operators.":http://qt-project.org/doc/qt-5/qdate.html#operator-lt So it should be ready to use. For instance
@
....
QDate today = QDate::currentDate();
QDate yesterday = date1.addDays ( -1 );if ( yesterday < today )
{
cout << "Yesterday was before today" << endl;
}
@The comparison is done on the Julian day number.
-
You can also put QDate into "QList and use qSort.":http://qt-project.org/doc/qt-5/qlist.html#fromSet as shown in the example, but substitute int with QDate
-
As QDate (and QDateTime) implements operator<, QDate can be used with any sorting algorithm you fancy. Including qSort, but the ones from std work just as well if not better.
-
since QDate support operator <
@
std::sort(std::begin(dates), std::end(dates));
@