# Sort Array Inventory by Amount

**URL:** <https://forums.unrealengine.com/t/sort-array-inventory-by-amount/444236>\
**Category:** Editor Scripting\
**Tags:** Struct, inventory, unreal-engine, Array, question\
**Created:** [February 27, 2019, 8:55am UTC](https://forums.unrealengine.com/t/sort-array-inventory-by-amount/444236 "2019-02-27T08:55:05Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![vinstriano](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@vinstriano](https://forums.unrealengine.com/u/vinstriano)\
**Post date:** [February 27, 2019, 8:55am UTC](https://forums.unrealengine.com/t/sort-array-inventory-by-amount/444236/1 "2019-02-27T08:55:05Z")

</div>

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**
```

---

<div class="post-metadata">

**Author:** ![KristofMorva](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/kristofmorva/32/1194508_2.png) [@KristofMorva](https://forums.unrealengine.com/u/KristofMorva)\
**Post date:** [February 27, 2019, 12:30pm UTC](https://forums.unrealengine.com/t/sort-array-inventory-by-amount/444236/2 "2019-02-27T12:30:12Z")

</div>

@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).

---

<div class="post-metadata">

**Author:** ![KristofMorva](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/kristofmorva/32/1194508_2.png) [@KristofMorva](https://forums.unrealengine.com/u/KristofMorva)\
**Post date:** [February 27, 2019, 12:42pm UTC](https://forums.unrealengine.com/t/sort-array-inventory-by-amount/444236/3 "2019-02-27T12:42:59Z")

</div>

I believe you can use [Rama’s Victory Plugin](https://forums.unrealengine.com/development-discussion/blueprint-visual-scripting/4014-39-rama-s-extra-blueprint-nodes-for-you-as-a-plugin-no-c-required) to sort by structure field ( `Amount` ).

---

<div class="post-metadata">

**Author:** ![Everynone](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/everynone/32/1135017_2.png) [@Everynone](https://forums.unrealengine.com/u/Everynone)\
**Post date:** [February 27, 2019, 12:53pm UTC](https://forums.unrealengine.com/t/sort-array-inventory-by-amount/444236/4 "2019-02-27T12:53:09Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![KristofMorva](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/kristofmorva/32/1194508_2.png) [@KristofMorva](https://forums.unrealengine.com/u/KristofMorva)\
**Post date:** [February 27, 2019, 1:08pm UTC](https://forums.unrealengine.com/t/sort-array-inventory-by-amount/444236/5 "2019-02-27T13:08:13Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Everynone](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/everynone/32/1135017_2.png) [@Everynone](https://forums.unrealengine.com/u/Everynone)\
**Post date:** [February 27, 2019, 1:12pm UTC](https://forums.unrealengine.com/t/sort-array-inventory-by-amount/444236/6 "2019-02-27T13:12:35Z")

</div>

> 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.

---

<div class="post-metadata">

**Author:** ![HarryHighDef](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@HarryHighDef](https://forums.unrealengine.com/u/HarryHighDef)\
**Post date:** [February 27, 2019, 1:13pm UTC](https://forums.unrealengine.com/t/sort-array-inventory-by-amount/444236/7 "2019-02-27T13:13:28Z")

</div>

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

 ![](https://d3kjluh73b9h9o.cloudfront.net/original/4X/1/8/d/18dd14a72206972a436c06555d9dd4f07a6f8b68.png)

---

<div class="post-metadata">

**Author:** ![HarryHighDef](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@HarryHighDef](https://forums.unrealengine.com/u/HarryHighDef)\
**Post date:** [February 27, 2019, 1:15pm UTC](https://forums.unrealengine.com/t/sort-array-inventory-by-amount/444236/8 "2019-02-27T13:15:36Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![KristofMorva](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/kristofmorva/32/1194508_2.png) [@KristofMorva](https://forums.unrealengine.com/u/KristofMorva)\
**Post date:** [February 27, 2019, 1:33pm UTC](https://forums.unrealengine.com/t/sort-array-inventory-by-amount/444236/9 "2019-02-27T13:33:05Z")

</div>

@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).

---

<div class="post-metadata">

**Author:** ![vinstriano](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@vinstriano](https://forums.unrealengine.com/u/vinstriano)\
**Post date:** [February 27, 2019, 4:20pm UTC](https://forums.unrealengine.com/t/sort-array-inventory-by-amount/444236/10 "2019-02-27T16:20:19Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![LatFoued](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@LatFoued](https://forums.unrealengine.com/u/LatFoued)\
**Post date:** [February 27, 2019, 4:50pm UTC](https://forums.unrealengine.com/t/sort-array-inventory-by-amount/444236/11 "2019-02-27T16:50:04Z")

</div>

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](https://www.unrealengine.com/marketplace/en-US/low-entry-extended-standard-library)

---

<div class="post-metadata">

**Author:** ![vinstriano](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@vinstriano](https://forums.unrealengine.com/u/vinstriano)\
**Post date:** [February 27, 2019, 7:56pm UTC](https://forums.unrealengine.com/t/sort-array-inventory-by-amount/444236/12 "2019-02-27T19:56:17Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![vinstriano](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@vinstriano](https://forums.unrealengine.com/u/vinstriano)\
**Post date:** [February 27, 2019, 7:58pm UTC](https://forums.unrealengine.com/t/sort-array-inventory-by-amount/444236/13 "2019-02-27T19:58:01Z")

</div>

![269824-sort.png](https://d3kjluh73b9h9o.cloudfront.net/original/4X/d/0/4/d043da9013eaf25f2d450d01271d784ee37cdc00.png)

Hope can help someone.

---

<div class="post-metadata">

**Author:** ![Nebula\_Games\_Inc](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Nebula\_Games\_Inc](https://forums.unrealengine.com/u/Nebula_Games_Inc)\
**Post date:** [February 28, 2019, 2:31am UTC](https://forums.unrealengine.com/t/sort-array-inventory-by-amount/444236/14 "2019-02-28T02:31:41Z")

</div>

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.

[![](https://d3kjluh73b9h9o.cloudfront.net/original/4X/9/1/e/91e1fa67803ac8905df01d1081e1109339b0dbf3.jpeg "Unreal Engine 4 Tutorial: "C++ For Blueprinters!" Sorting Arrays") ](https://www.youtube.com/watch?v=ZqO0Vf5SF6k)

![269853-integer-sort.jpg](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/3/5/235222da373310db59022590e76f2a9cae293028.jpeg)

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_b9fddd0c](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_b9fddd0c](https://forums.unrealengine.com/u/anonymous_user_b9fddd0c)\
**Post date:** [December 2, 2020, 1:32pm UTC](https://forums.unrealengine.com/t/sort-array-inventory-by-amount/444236/15 "2020-12-02T13:32:40Z")

</div>

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

 ![323671-nuova-immagine-bitmap.jpg](https://d3kjluh73b9h9o.cloudfront.net/original/4X/7/6/a/76adb437bb329d776b8c0ce0a41a7ac93a77574e.jpeg)

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_b9fddd0c](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_b9fddd0c](https://forums.unrealengine.com/u/anonymous_user_b9fddd0c)\
**Post date:** [December 3, 2020, 7:20pm UTC](https://forums.unrealengine.com/t/sort-array-inventory-by-amount/444236/16 "2020-12-03T19:20:28Z")

</div>

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

 ![323671-nuova-immagine-bitmap.jpg](https://d3kjluh73b9h9o.cloudfront.net/original/4X/7/6/a/76adb437bb329d776b8c0ce0a41a7ac93a77574e.jpeg)
