Problem with sorting array

Hi,

I have (again) a problem with arrays. :o
This time its about sorting an array.
Since there is no sorting functionality for arrays, it needs to be implemented in the graph. Also, since arrays dont have a “swap elements” function, that has to be done in BP.

As Im only sorting arrays rather small in size, the good old bubble sort will suffice.
But it doesnt work :frowning:

Here is what I did:

This is the array, the Skills. This function just returns a data structure with some random values:

This is how the whole thing is called from the event graph. just putting some text out with the contents of the array (that should be sorted).

During gameplay, this is what I get:

So, obviously not only doesnt get the data sorted, but also the data gets messed up. All of a sudden there are 3 Combat, or 2 support skills there. :confused:

The GetBestFit function is suppoesed to handle the bubble sort… First some initial values are stored local (as Zack Parrish would :D)
It should be noted that the Skills input is passed as reference.

Then, I loop through the array until, at the end of the loop, the IsSorted value (which is intialized to true right before the loop) stayed true after the loop completes.
Well, its bubble sort, you know…

8aa008f0fcca6c37306a54fe8fad5fa1010e3733.jpeg

The loop itself looks like this:

I get some index values and the corresponding data elements. Then I compare the skill value.

The sorting should be in descending order, so in case The current element skill value is greater than the next element skill value, we do nothing, those two are already sorted.
Else, the items get swapped and sorted is set to false, which will cause the loop to be repeated util no swap was necessary, which means the array is sorted.

8377920578430f6f4be669422cab29e180cc7dff.jpeg

Alas, arrays dont have a swap function, here its is:
Pretty straight forward. I get the elements and set one for another.
It should be noted that, again, the function data input is passed as reference.

c3142af8de47e6d6559117a3c7c3f94ba1d39c96.jpeg

But, as you can see from the log, it just doesnt work :frowning:

Is there something wrong with my graph logic :confused:
Am I assigning something wrong somewhere :confused:
Sorting doesnt work, thats one thing, but why does my data gets messed up :confused:

Any help is greatly appreciated :slight_smile:

Cheers,
Klaus

I can’t remember where but im pretty sure somebody added in a sort array blueprint node plugin that you can get. Might have to do a bit of digging to find it though.

Thanks for the suggestion :slight_smile:
But Id rather like to do this without a plugin.
Once for a learning effect and secondly also not being dependent on someone elses plugins (and their maintenance).

But I still cant figure out what going wrong :frowning:

Try printing what ItemRefA and B are after each set array elem.

Here is what I did:

The swap function now prints ou the reference data:

ef5da6415995641a57728d0dadbeedb736c7e223.jpeg

And the MakeStats function now supplies steady values for easier debugging (instead of random floats)
Those are the test values:

f562d3147027bbff229ecdc711bc200222e88672.jpeg

During gameplay, the log now shows:

Which somehow seems to do the sorting right all of a sudden. :eek:

Is is perhaps possible that the array input of the loop is “polled” from its connected source for each iteration of the foor loop?

Like:

Somehow that seems too odd to be true.
However it would explain why the data got garbled up as for each iteration a complete new random set of stats was created.

Im gonna test some more on this and then simply call the sorting function right in the MakeStats function so they get emitted already sorted in the first place.

Still, can somebody shed some light on node behavior?
When keep nodes their data and when do they refetch it each time they are polled :confused:

Thanks for any help :smiley:

Cheers,
Klaus