Sort Array Inventory by 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.