# Trying to sort an array by an integer variable

**URL:** <https://forums.unrealengine.com/t/trying-to-sort-an-array-by-an-integer-variable/2606069>\
**Category:** Programming & Scripting\
**Tags:** unreal-engine, question\
**Created:** [July 15, 2025, 11:05pm UTC](https://forums.unrealengine.com/t/trying-to-sort-an-array-by-an-integer-variable/2606069 "2025-07-15T23:05:15Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Stevie420](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Stevie420](https://forums.unrealengine.com/u/Stevie420)\
**Post date:** [July 15, 2025, 11:05pm UTC](https://forums.unrealengine.com/t/trying-to-sort-an-array-by-an-integer-variable/2606069/1 "2025-07-15T23:05:15Z")

</div>

So i have an initiative system like dungeons and dragons or baldurs gate 3. When combat is called, all BP\_BaseCharacter’s do the initiative roll function. This sets the integer variable “InitiativeSet” and then sends that actor to an Array in a gamestate blueprint i have called CombatState. I then want this array of actors to be sorted by their “InitiativeSet” variable. Is this possible in blueprints? Or is there another way i could accomplish this goal?

 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/f/1/2f1486e79de250732502ada4106cdf563c495520.jpeg)

---

<div class="post-metadata">

**Author:** ![nande](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/nande/32/1057698_2.png) [@nande](https://forums.unrealengine.com/u/nande)\
**Post date:** [July 15, 2025, 11:21pm UTC](https://forums.unrealengine.com/t/trying-to-sort-an-array-by-an-integer-variable/2606069/2 "2025-07-15T23:21:52Z")

</div>

this is possible. but my experience tells me it’s better not to sort the actors list as you might want to use it in other places for other purposes.  
you can do this.  
treat each index in that array as an “id”. (which means don’t move the actor in the actor list, and if you do, consider your ids invalid).

then when iterating just use an list of the order.  
for example. you have 5 actors  
actors = { a ,b,c,d,e}  
then you have a list of their initiatives, use their ids as values  
init = {0,1,2,3,4}  
that’s the default order. you do your math to come up with the correct order, like  
init = {4,3,2,1,0}

then when you need to iterate them, you use it through the initiatives list

num\_inits = init.Num() (or size, this size should be the same as actors)  
for i in 0..num\_inits:  
id = init[i]  
act = actors[id]  
do whatever you need here

that way your sort will be faster since it doesn’t copy large objects (though actors are probably pointers so it would be fast).  
but also allows you to iterate the actors by initiative and also by regular order.  
so you can still process them in the ui correctly. for example .

(it’s been quite a while since i had the pleasure to play d&d i hope i got it right)

---

<div class="post-metadata">

**Author:** ![AlienRenders](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@AlienRenders](https://forums.unrealengine.com/u/AlienRenders)\
**Post date:** [July 16, 2025, 5:08am UTC](https://forums.unrealengine.com/t/trying-to-sort-an-array-by-an-integer-variable/2606069/3 "2025-07-16T05:08:43Z")

</div>

Here’s a post with a bunch of examples on implementing lots of different sorting algorithms in blueprint. Just change the Blueprint after the GET nodes to grab your InitiativeSet variable. Quicksort is probably the best, but if you have a small array (under 20 or 30 items), insertion sort is probably good enough.

There’s even a zip file with the blueprints.

> [@Sorting algorithms in Blueprints](https://forums.unrealengine.com/t/sorting-algorithms-in-blueprints/1135992):
>
> It is common knowledge that blueprints do not include sorting functions. In my previous experiences, I have been able to create simple sorting functions easily. However, during my current project, I faced the challenge of sorting large data arrays and found that the sorting process was either very slow or unsuccessful. Since I wanted to avoid using C++ code or third-party development, I independently researched this issue. After conducting some research, I have decided to share my findings in t…

If you have access to C++ in your project and your InitiativeSet variable is defined in C++, I’d use C++ as it’s way easier and faster than anything in blueprints.

```auto
void SortByInitiative(TArray<MyActor*> &Actors)
{
  Actors.Sort([](const MyActor *A, const MyActor *B) -> bool
  {
    return A->InitiativeSet < B->InitiativeSet;
  }
}

```

Use \> (greater than) instead if you want to go from large to low numbers.

This would be callable by your blueprint if you put the ‘BlueprintCallable’ tag in your UFUNCTION metadata. Make the function static and it’s callable from any blueprint.
