Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to order a list of dates
Forum Updated to NodeBB v4.3 + New Features

How to order a list of dates

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 500 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    blackout69
    wrote on 17 Jul 2021, 22:36 last edited by
    #1

    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 code

    ui->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

    O 1 Reply Last reply 17 Jul 2021, 22:58
    0
    • B blackout69
      17 Jul 2021, 22:36

      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 code

      ui->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

      O Offline
      O Offline
      ODБOï
      wrote on 17 Jul 2021, 22:58 last edited by
      #2

      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

      1 Reply Last reply
      2
      • B Offline
        B Offline
        blackout69
        wrote on 18 Jul 2021, 09:21 last edited by
        #3

        Thank you ODБOï,
        your suggestion was invaluable.
        I solved it this way

        dates.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 Reply Last reply
        1

        1/3

        17 Jul 2021, 22:36

        • Login

        • Login or register to search.
        1 out of 3
        • First post
          1/3
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved