Download

Blueprint Bubblesort doesn't work correctly

Hey there.

I hope Code Review/Assistance is allowed here.

I have an array of Integers. These Integers are actually indices of an array.
These indices point indirectly onto items. These Items have an ID. The *indices *should be sorted by this ID.
For a test, I have 4 Items with the IDs
1
2
4
1021

But after sorting, the indices are
1021
1
2
4

I’m pretty sure my Bubblesort is ok and does not have any errors.
54c0984ec2e7770a80378a77a16adad210eb9399.jpeg
This is the Comparator

577898cbc08136e98eceecd8dd0dec6b7543aa62.png
This is the Sort

Did I miss something? Becaus I feel pretty stupid at the moment.

You didn’t attach the B node:

There’s a get max value of array function in blueprint that you can use to simplify the sorting immensely. Here’s an example setup (not mine).

https://answers.unrealengine.com/storage/attachments/30792-sort-float-array-descending.jpg

Obviously you’d need to tweak this to build the correct array, but you get the idea.

It is attached, it’s just a reroute node.

I’m not sorting the int array itself. so this doesn’t help me.

The content of this array are actually Indices of an array.

IndexArray->Int->Index of Item in the Inventory -> Index -> Item -> ID

I want to sort the Items by ID.

As long as you perform the operation on the correct array, or set of arrays you can still do that easily.

Check out this picture:

cee25495b8dd0429bab00ab68f1b41be3e29e1ab.png

“Raw Distance” is an array of floats which corresponds to the array “Chunks”.

After I loop through this, ChunkIDByDist will be sorted in ascending order by the values in “Raw Distance”.

I guess you misunderstand me. I don’t have an array of ints or something like that.

Unbenannt.jpg

I want to sort the Indices (left) by the ID (green)

Looks like this could potentially try to access an index greater than the length of the array. The first setarrayelem has an opportunity to set Item to array.get(arraylength), which is the element after the last element in the array. I tried implementing these nodes and blueprint would crash silently.

By comparing A with B, but swapping A with A+1 (which could be completely different index from B), I’m not sure the result is actually getting sorted.

And then you need to run the bubble sort repeatedly, because one pass of bubble sort won’t be enough.