Sorting array elements

I have array of actors (buildings, minions, characters), how do i sort array to have minions at the top, buildings in the middle and characters in the bottom?

Best method for this would be Insertion Sort, to make sure you put them into the array at the right position instead of doing it after the fact. Basically it goes each time you add something to the array you search it and find the correct position, in this case you would be looking for a class comparison and if its the right spot insert it into the array at that position shifting the other items index.

Maybe this method isnt good for you though, you might need to provide abit more details on what youre trying to accomplish with said array, cool.

Well, i have minion with perception, it may perceive a bunch of different actors like other minions, buildings, neutrals and characters, so i need it to attack perceived actors in this exact order

Well you dont need to sort the array to be able to achieve this, you could filter the array when youre looking for a target to attack based on that criteria. Im guessing your list is only for whats in the immediate vicinity of the minion?

Im wondering what type of order they are in when they come in, if they are distance based or something else. If its perception then its possible the nearest is prioritized and searching the array for the first available minion will be very fast already.

That is some massively complex function you have there!! Check out this solution, you might like it…I don’t know if yours does anything else besides sort actors by distance but at least that part I have a solution for you that will be much more efficient. It will create a sorted list of near-to-far or far-to-near :slight_smile:

Here is a blueprint I did to sort closest actors. If you take a look, I sort them into two arrays (Less than and Greater than), you could modify this same concept and add another array to have 3 and then merge them as I did into one array at the end. Hope this helps.

That’s nice and all, but then you have to install Visual Studio. It would have been great if Epic would have had this pre-existing in blueprints since a native C++ Function exists. Nice Tutorial. The next game I do I might get into C++. For now I don’t want to have to install or use visual studio so had to make my own Blueprint version. It is basically the same thing although I’m not sure what there sorting method is. And it’s probably faster but oh well lol. It was a learning experience making it.

Yea, I did something similar for an array where I was trying to find a particular item, made this complex thing only to realize there is a “Contains” node. You learn a lot that way though ha