Sort Array Inventory by Amount

Hi, i’m working on an inventory system and i need “update inventory”, how can I order my STRUCT Array by deleting the indexes that have the amount 0 when it’s used by the player? It’s 2 days I’m trying to find something on the forum but I can not solve this problem… can you help me please? thanks in advance! Any suggestion please.

Example:
Item 1: Amount **5**
Item 2: Amount **4**
Item 3: Amount **0**
Item 4: Amount **0**
Item 5: Amount **1**

I need this:
Item 1: Amount **5**
Item 2: Amount **4**
Item 3: Amount **1**
Item 4: Amount **0**
Item 5: Amount **0**

What exactly won’t work well about it? Before removing, add the entry to a temp array and append once completed.

You’re removing elements from the same
array you’re iterating over. It’ll
shrink the size of the array, but the
loop counter inside ForEachLoop won’t
decrease, so it’ll skip elements.

To quote the description of the macro:
Warning: Do not add, remove, or
reorder elements in the input array
during the execution of the loop
body!, which is exactly what you’re
doing.

I see what you mean, you’re right. So flag the index for removal once they’re all discovered. There are so many ways to achieve that.

I’ll remove my original answer, it’s not correct as is.

@Everynone It won’t work well, as you’re removing elements from the array in the loop (just like it’s stated inside the ForEachLoop macro).

I believe you can use Rama’s Victory Plugin to sort by structure field ( Amount ).

You’re removing elements from the same array you’re iterating over. It’ll shrink the size of the array, but the loop counter inside ForEachLoop won’t decrease, so it’ll skip elements.

To quote the description of the macro: Warning: Do not add, remove, or reorder elements in the input array during the execution of the loop body!, which is exactly what you’re doing.

I’m not saying it cannot be avoided or fixed, but as-is, your example will result in unexpected consequences for people who will copy it.

@Everynone yes you’re right, there are many ways to achieve it, I just wanted to prevent people learning from an example they should avoid, thanks for the response.

@HarryHighDef that’s right, using a ReverseForEachLoop usually bypasses the problem (or creating a copy of the original array, or an index array for removals as @Everynone said).

This should do the trick. If you don’t want zero value amounts you can just not add them

You can sometimes get away with it by iterating through the array backwards. but It depends on how you are using it.

Great Idea Harry, thank you i will try it!
Thats why i love this community, thank you all so much!

You can just create a local temp variable in the function that holds the original array, iterate through that copy and remove from the original array.
You can also use Rama’s plugin or the LE Extended Standard Library

It was not exactly what I needed but I managed to solve thanks to the idea of ​​Harry, thank you all!

Hope can help someone.

Doesn’t work for a “struct” but if anyone is trying to simply sort an array of integers this will accomplish that in one neat little node. Link to the tutorial to create the node is below (just copy/paste C++ code). This will also give you a String sort and a Float sort.

269853-integer-sort.jpg

You can just do a reverse loop removing index with 0 quantity and then if you want the lenght of the array to stay the same just resize it as before.

Remove Index will delete the array item and push the remaining like:

index0 Item1

index1 Item2

index2 Item3

index3 Item4

Remove Index(1)

index0 Item1

index1 Item3

index2 Item4

You can just do a reverse loop removing index with 0 quantity and then if you want the lenght of the array to stay the same just resize it as before.

Remove Index will delete the array item and push the remaining like:

index0 Item1

index1 Item2

index2 Item3

index3 Item4

Remove Index(1)

index0 Item1

index1 Item3

index2 Item4