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 list of structure based on specific ID parameter
QtWS25 Last Chance

Sorting list of structure based on specific ID parameter

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 4 Posters 320 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
    lukutis222
    wrote on last edited by lukutis222
    #1

    Hello. I have a list of structure declared in my .h file:

    struct cursor_s{
            uint8_t id;
            float time;
            float current;
            float voltage;
    };
    QList<cursor_s> cursor_list_s;
    

    This list is being filled up during the execution of the program:

    cursor_s new_cursor;
    new_cursor.id = cursor_counter;
    new_cursor.time = time;
    new_cursor.current = current;
    new_cursor.voltage = voltage;
    cursor_list_s.push_back(new_cursor);
    

    cursor_counter is a variable I use to identify the number of the cursor added to the list. For example, after 5 cursors have been added in a row, I will have a list of structure as following:

    cursor id = 0
    cursor time = 38.49
    cursor current = 5.34
    cursor voltage = 2.67
    
    cursor id = 1
    cursor time = 114.62
    cursor current = 4.80
    cursor voltage = 2.40
    
    cursor id = 2
    cursor time = 167.33
    cursor current = 5.00
    cursor voltage = 2.50
    
    cursor id = 3
    cursor time = 198.28
    cursor current = 5.05
    cursor voltage = 2.53
    
    cursor id = 4
    cursor time = 227.84
    cursor current = 5.00
    cursor voltage = 2.50
    

    I can then choose and delete any item from the list. For example, if I delete 2nd structure from the structure list using the function:

     cursor_list_s.removeAt(1);
    

    I will be left with:

    cursor id = 0
    cursor time = 38.49
    cursor current = 5.34
    cursor voltage = 2.67
    
    cursor id = 2
    cursor time = 167.33
    cursor current = 5.00
    cursor voltage = 2.50
    
    cursor id = 3
    cursor time = 198.28
    cursor current = 5.05
    cursor voltage = 2.53
    
    cursor id = 4
    cursor time = 227.84
    cursor current = 5.00
    cursor voltage = 2.50
    

    I need a method that would rearrange the order of my struct based on ID parameter in ascending order every time an item has been removed. So looking at the example I have provided above, cursor_list_s[1], cursor_list_s[2] and cursor_list_s[3] would have to be rearranged and their ID's changed to -1

    Perhaps someone could suggest what would be clean and simple method to do this?

    C JonBJ 2 Replies Last reply
    0
    • L lukutis222

      Hello. I have a list of structure declared in my .h file:

      struct cursor_s{
              uint8_t id;
              float time;
              float current;
              float voltage;
      };
      QList<cursor_s> cursor_list_s;
      

      This list is being filled up during the execution of the program:

      cursor_s new_cursor;
      new_cursor.id = cursor_counter;
      new_cursor.time = time;
      new_cursor.current = current;
      new_cursor.voltage = voltage;
      cursor_list_s.push_back(new_cursor);
      

      cursor_counter is a variable I use to identify the number of the cursor added to the list. For example, after 5 cursors have been added in a row, I will have a list of structure as following:

      cursor id = 0
      cursor time = 38.49
      cursor current = 5.34
      cursor voltage = 2.67
      
      cursor id = 1
      cursor time = 114.62
      cursor current = 4.80
      cursor voltage = 2.40
      
      cursor id = 2
      cursor time = 167.33
      cursor current = 5.00
      cursor voltage = 2.50
      
      cursor id = 3
      cursor time = 198.28
      cursor current = 5.05
      cursor voltage = 2.53
      
      cursor id = 4
      cursor time = 227.84
      cursor current = 5.00
      cursor voltage = 2.50
      

      I can then choose and delete any item from the list. For example, if I delete 2nd structure from the structure list using the function:

       cursor_list_s.removeAt(1);
      

      I will be left with:

      cursor id = 0
      cursor time = 38.49
      cursor current = 5.34
      cursor voltage = 2.67
      
      cursor id = 2
      cursor time = 167.33
      cursor current = 5.00
      cursor voltage = 2.50
      
      cursor id = 3
      cursor time = 198.28
      cursor current = 5.05
      cursor voltage = 2.53
      
      cursor id = 4
      cursor time = 227.84
      cursor current = 5.00
      cursor voltage = 2.50
      

      I need a method that would rearrange the order of my struct based on ID parameter in ascending order every time an item has been removed. So looking at the example I have provided above, cursor_list_s[1], cursor_list_s[2] and cursor_list_s[3] would have to be rearranged and their ID's changed to -1

      Perhaps someone could suggest what would be clean and simple method to do this?

      C Offline
      C Offline
      ChrisW67
      wrote on last edited by
      #2

      @lukutis222 The order of the items that remain in the list has not changed so you do not need to sort or reorder the list at all.

      If you want to the change the ID value of each struct so they are numbered contiguously 0..n then you can do that with a simple loop.

      for (int i = 0; i < cursor_list_s.count(); ++i) {
        cursor_list_s[i].id = i;
      }
      

      However, if that is what you really want to do, then you could just remove the ID from the struct entirely: it just mimics the index in the array anyway.

      In the same vein, cursor_counter is the same as cursor_list_s.count() or cursor_list_s.count()-1

      1 Reply Last reply
      1
      • L lukutis222

        Hello. I have a list of structure declared in my .h file:

        struct cursor_s{
                uint8_t id;
                float time;
                float current;
                float voltage;
        };
        QList<cursor_s> cursor_list_s;
        

        This list is being filled up during the execution of the program:

        cursor_s new_cursor;
        new_cursor.id = cursor_counter;
        new_cursor.time = time;
        new_cursor.current = current;
        new_cursor.voltage = voltage;
        cursor_list_s.push_back(new_cursor);
        

        cursor_counter is a variable I use to identify the number of the cursor added to the list. For example, after 5 cursors have been added in a row, I will have a list of structure as following:

        cursor id = 0
        cursor time = 38.49
        cursor current = 5.34
        cursor voltage = 2.67
        
        cursor id = 1
        cursor time = 114.62
        cursor current = 4.80
        cursor voltage = 2.40
        
        cursor id = 2
        cursor time = 167.33
        cursor current = 5.00
        cursor voltage = 2.50
        
        cursor id = 3
        cursor time = 198.28
        cursor current = 5.05
        cursor voltage = 2.53
        
        cursor id = 4
        cursor time = 227.84
        cursor current = 5.00
        cursor voltage = 2.50
        

        I can then choose and delete any item from the list. For example, if I delete 2nd structure from the structure list using the function:

         cursor_list_s.removeAt(1);
        

        I will be left with:

        cursor id = 0
        cursor time = 38.49
        cursor current = 5.34
        cursor voltage = 2.67
        
        cursor id = 2
        cursor time = 167.33
        cursor current = 5.00
        cursor voltage = 2.50
        
        cursor id = 3
        cursor time = 198.28
        cursor current = 5.05
        cursor voltage = 2.53
        
        cursor id = 4
        cursor time = 227.84
        cursor current = 5.00
        cursor voltage = 2.50
        

        I need a method that would rearrange the order of my struct based on ID parameter in ascending order every time an item has been removed. So looking at the example I have provided above, cursor_list_s[1], cursor_list_s[2] and cursor_list_s[3] would have to be rearranged and their ID's changed to -1

        Perhaps someone could suggest what would be clean and simple method to do this?

        JonBJ Online
        JonBJ Online
        JonB
        wrote on last edited by
        #3

        @lukutis222
        As @ChrisW67 has written. But why

        cursor_list_s[1], cursor_list_s[2] and cursor_list_s[3] would have to be rearranged and their ID's changed to -1

        anyway, what is "changing them to -1" all about?

        C 1 Reply Last reply
        0
        • JonBJ JonB

          @lukutis222
          As @ChrisW67 has written. But why

          cursor_list_s[1], cursor_list_s[2] and cursor_list_s[3] would have to be rearranged and their ID's changed to -1

          anyway, what is "changing them to -1" all about?

          C Offline
          C Offline
          ChrisW67
          wrote on last edited by
          #4

          @JonB I think the OP means reduce their ID by one so that the sequence is once again gap-free.

          JonBJ Christian EhrlicherC 2 Replies Last reply
          1
          • C ChrisW67

            @JonB I think the OP means reduce their ID by one so that the sequence is once again gap-free.

            JonBJ Online
            JonBJ Online
            JonB
            wrote on last edited by
            #5

            @ChrisW67 Oh, "by" -1 rather than "to" -1, OK!

            1 Reply Last reply
            0
            • C ChrisW67

              @JonB I think the OP means reduce their ID by one so that the sequence is once again gap-free.

              Christian EhrlicherC Online
              Christian EhrlicherC Online
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @ChrisW67 So iterating over a container is the question here? Seems to be complicated.

              Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
              Visit the Qt Academy at https://academy.qt.io/catalog

              C 1 Reply Last reply
              0
              • Christian EhrlicherC Christian Ehrlicher

                @ChrisW67 So iterating over a container is the question here? Seems to be complicated.

                C Offline
                C Offline
                ChrisW67
                wrote on last edited by
                #7

                @Christian-Ehrlicher The OP seems to want to add a series of consecutively numbered items to a list (the the number is "id"), remove arbitrary items, and have the items remaining still be consecutively numbered. Iterating over the list is solution to the question posed, but I think this is an XY Problem.

                This numbering:

                • repeats information that is available simply from the index in the list,
                • negates the typical purpose of an immutable identifier, and
                • enforces a common anti-pattern that monotonically numbered items need to be/stay gap free, in this case causing more busy work.
                1 Reply Last reply
                0

                • Login

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