Sorting list of structure based on specific ID parameter
-
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?
-
@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()
orcursor_list_s.count()-1
-
@lukutis222
As @ChrisW67 has written. But whycursor_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? -
@ChrisW67 So iterating over a container is the question here? Seems to be complicated.
-
@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.