How to order a list of dates
-
wrote on 17 Jul 2021, 22:36 last edited by
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
wrote on 17 Jul 2021, 22:58 last edited byhi
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 -
wrote on 18 Jul 2021, 09:21 last edited by
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
1/3