How to order a list of dates
-
Hello everybody,
after entering a list of dates in a QListWidget, I would like to sort them based on the date.
I'm using this simplified codeui->list->addItem("14-07-2021"); ui->list->addItem("27-06-2021"); ui->list->addItem("23-07-2021"); ui->list->sortItems();
But I see that it orders according to the day
14-07-2021
23-07-2021
27-06-2021
and not as I would, according to the date.
Can anyone tell me a way to order based on the date?
Thank you in advance.blackout69
-
Hello everybody,
after entering a list of dates in a QListWidget, I would like to sort them based on the date.
I'm using this simplified codeui->list->addItem("14-07-2021"); ui->list->addItem("27-06-2021"); ui->list->addItem("23-07-2021"); ui->list->sortItems();
But I see that it orders according to the day
14-07-2021
23-07-2021
27-06-2021
and not as I would, according to the date.
Can anyone tell me a way to order based on the date?
Thank you in advance.blackout69
hi
you can use QDateTime or QDate to construct date objects from string then sort them using operators listed here https://doc.qt.io/qt-5/qdatetime.html#operator-lt
or
https://stackoverflow.com/questions/5697505/how-do-i-sort-a-qlist-of-qdatetime -
Thank you ODБOï,
your suggestion was invaluable.
I solved it this waydates.append(date); dates.append(date); dates.append(date); std::sort(dates.begin(), dates.end()); for (int i = 0; i < dates.count(); ++i) { ui->list->addItem(dates[i].toString("dd-MM-yyyy")); }
blackout69