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. Sorting by integer in a structure contained in a QList or QVector
Forum Updated to NodeBB v4.3 + New Features

Sorting by integer in a structure contained in a QList or QVector

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 1.6k Views
  • 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.
  • L Offline
    L Offline
    leinad
    wrote on 5 May 2022, 11:32 last edited by
    #1

    Hi,

    This may be more of a C++ questions as appose to Qt but the is some overlap. I have a structure which is made up of an integer and a QString.

    struct myStruct {
    int seq;
    QString someText;
    }
    I then add the structure in a QList or QVector (I guess it doesn't make a difference), like so,
    QVector<myStruct> myVector;
    So now I have a vector of structures and each structure has an integer and QString elements.

    As I add values to the vector, the seq (integer value) may or may not be in order. I know if I have a QList<int> I can easily sort it. Is there a way that is similar to sort where I can rearrange the order of my structure entries in the vector so they are in order by integer value (seq)? I guess, I can copy the seq into a QList, sort it and then use a for loop through the vector and match the seq between the QList and QVector and rearrange that way but it seems like a complicated convoluted way of doing it.

    Thanks.

    J 1 Reply Last reply 5 May 2022, 12:22
    0
    • L leinad
      5 May 2022, 11:32

      Hi,

      This may be more of a C++ questions as appose to Qt but the is some overlap. I have a structure which is made up of an integer and a QString.

      struct myStruct {
      int seq;
      QString someText;
      }
      I then add the structure in a QList or QVector (I guess it doesn't make a difference), like so,
      QVector<myStruct> myVector;
      So now I have a vector of structures and each structure has an integer and QString elements.

      As I add values to the vector, the seq (integer value) may or may not be in order. I know if I have a QList<int> I can easily sort it. Is there a way that is similar to sort where I can rearrange the order of my structure entries in the vector so they are in order by integer value (seq)? I guess, I can copy the seq into a QList, sort it and then use a for loop through the vector and match the seq between the QList and QVector and rearrange that way but it seems like a complicated convoluted way of doing it.

      Thanks.

      J Offline
      J Offline
      JonB
      wrote on 5 May 2022, 12:22 last edited by JonB 5 May 2022, 12:23
      #2

      @leinad
      Sort QVector of pointers to custom objects shows just your situation. It shows you how to use std::sort() on a QVector, which is what you are expected to do. That example stores/sorts a vector of pointers to struct/class, but you can change it to work on a vector of the struct/class instead of pointers.

      An older post in this forum, https://forum.qt.io/topic/47037/solved-sorting-possible-on-a-qvector-that-has-a-struct-as-elements, shows the same approach but for QVector<struct> like your case. It uses qsort() because it's old, you should use std::sort().

      1 Reply Last reply
      3
      • L Offline
        L Offline
        leinad
        wrote on 5 May 2022, 13:19 last edited by
        #3

        Thank you. I'll take a look

        1 Reply Last reply
        0

        1/3

        5 May 2022, 11:32

        • 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