Download

Is there any kind of Order/Sort node for Arrays?

My very basic car AI is almost done, but I’ve noticed that the array that contains the value for the target objects is randomizing its order, appearently, at least in a coding side, I would need to sort it, but no luck trying to find that in BP, or do I have to do it in another way?

Don’t think there is a node. What do you hope to sort them by?

I would need to sort the target object by number

Ex: TargetPoint1, TargetPoint2, TargetPoint3…

The Array itself will sort it randomized, I need them organized .

https://youtu.be/Ckj4UN374lw

Not sure how to do it by target name as its a string… But here’s an old video how I sorted by a stat from an array.

All are named TargetPointXY? Well, you can convert the string to a character array (there is a node for this but I don’t know the actual name atm). That character array is a string array where each element contains exactly one character. So in that array at element index 10 you can extract the number by converting it to an integer. In case you have more than 9 TargetPoints you will need to do some extra work with multiplying the element at index 10 with 10 and adding the element at index 11 and so on.

This is obviously a little complicated but blueprints aren’t good in this kind of things. Can’t you add the index to the respective TargetPoint when you create it?

To the sorting part. An easy way to sort an array in blueprints is to create a ForLoop. Plug Array->LastIndex into LastIndex of the ForLoop. In the LoopBody use the node MaxOfIntArray (or MinOfIntArray) on the array with your indices. Add the element of the TargetPoint array at the index the MaxOfIntArray returns to a new array of TargetPoints. Afterwards in the LoopBody remove the elements of the old TargetPoints array and the old Indices array at that index. The result is a new array with sorted TargetPoints.

This is a super easy sorting algorithm but how many elements does your array have and how often are you sorting it? You shouldn’t use such an algorithm when sorting many elements frequently.

Strings can be compared as they are, as long as you number them all with the same number of digits.
Target001, Target002, …
Else, Target2 will sort after Target11.

Anyway, no, there is no generic node to sort an array. Probably because there exists no generic “function as argument” data type in Blueprints, which means that you can’t pass a “comparator” into the function, and Blueprint does not have generics in the sense that it would compare “anything.”
It would be very easy to write as a C++ blueprint plugin / function library, though. Like, really easy, if you know any C++ at all and have Visual Studio installed.

I really don’t know C++ (no language actually), and neither I have the VS installed.

Decided to download LE Extended Library and decided to take a look at the source code

I’m really dumb at programming, but is the whole Sort thing just a few lines of comparing A > B or A < B?

Basically yes. But that would most likely be a native algorithm. There are more advanced ones that do the job way
faster. Otherwise you would compare each entry with all other entries. There are also ways that only go once through
the array.

You could simply google for “Bubble Sort” for example and convert the algorithms you find on wikipedia
into a BP function.

Looking here, isn’t bubble sort quite inneficient? Althrough it will rearrange in order, it will take N loops to make it into the right order, an traditional approach would rearrange the whole thing as you press play.

Oh it certainly is inefficient, but I didn’t know how well you can handle more complex code :x

Sorry, tought you were recomending it for my case.

Anyway, decided to use a friend method, create an int variable for the target as a number order and increase for the next one when it collides.

in case someone need it

In case someone need it :o