# Blueprint Bubblesort doesn't work correctly

**URL:** <https://forums.unrealengine.com/t/blueprint-bubblesort-doesnt-work-correctly/70040>\
**Category:** Blueprint\
**Tags:** unreal-engine\
**Created:** [July 27, 2016, 4:10pm UTC](https://forums.unrealengine.com/t/blueprint-bubblesort-doesnt-work-correctly/70040 "2016-07-27T16:10:33Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Raildex](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Raildex](https://forums.unrealengine.com/u/Raildex)\
**Post date:** [July 27, 2016, 4:10pm UTC](https://forums.unrealengine.com/t/blueprint-bubblesort-doesnt-work-correctly/70040/1 "2016-07-27T16:10:33Z")

</div>

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](https://d3kjluh73b9h9o.cloudfront.net/original/4X/5/4/c/54c0984ec2e7770a80378a77a16adad210eb9399.jpeg)  
This is the Comparator

 ![Comparator.jpg](https://d3kjluh73b9h9o.cloudfront.net/original/4X/5/4/c/54c0984ec2e7770a80378a77a16adad210eb9399.jpeg)  
This is the Sort

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

---

<div class="post-metadata">

**Author:** ![MrGoatsy](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/mrgoatsy/32/918697_2.png) [@MrGoatsy](https://forums.unrealengine.com/u/MrGoatsy)\
**Post date:** [July 27, 2016, 8:04pm UTC](https://forums.unrealengine.com/t/blueprint-bubblesort-doesnt-work-correctly/70040/2 "2016-07-27T20:04:47Z")

</div>

You didn’t attach the B node:  
[![](https://i.imgur.com/BJ7q9tk.png) ](https://i.imgur.com/BJ7q9tk.png)

---

<div class="post-metadata">

**Author:** ![Hyperloop](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/hyperloop/32/1203427_2.png) [@Hyperloop](https://forums.unrealengine.com/u/Hyperloop)\
**Post date:** [July 27, 2016, 8:31pm UTC](https://forums.unrealengine.com/t/blueprint-bubblesort-doesnt-work-correctly/70040/3 "2016-07-27T20:31:07Z")

</div>

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).

 ![30792-sort-float-array-descending.jpg](https://d3kjluh73b9h9o.cloudfront.net/original/4X/f/4/0/f408e035f0decb92a8d81658aaa0a7a912d2888a.jpeg)

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

---

<div class="post-metadata">

**Author:** ![Raildex](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Raildex](https://forums.unrealengine.com/u/Raildex)\
**Post date:** [July 27, 2016, 9:31pm UTC](https://forums.unrealengine.com/t/blueprint-bubblesort-doesnt-work-correctly/70040/4 "2016-07-27T21:31:36Z")

</div>

> [@MrGoatsy;571757](#):
>
> You didn’t attach the B node:  
> [http://i.imgur.com/BJ7q9tk.png](http://i.imgur.com/BJ7q9tk.png)

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

> [@Hyperloop;571773](#):
>
> 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).
> 
> ![30792-sort-float-array-descending.jpg](https://d3kjluh73b9h9o.cloudfront.net/original/4X/f/4/0/f408e035f0decb92a8d81658aaa0a7a912d2888a.jpeg)
> 
> Obviously you’d need to tweak this to build the correct array, but you get the idea.

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.

---

<div class="post-metadata">

**Author:** ![Hyperloop](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/hyperloop/32/1203427_2.png) [@Hyperloop](https://forums.unrealengine.com/u/Hyperloop)\
**Post date:** [July 27, 2016, 9:42pm UTC](https://forums.unrealengine.com/t/blueprint-bubblesort-doesnt-work-correctly/70040/5 "2016-07-27T21:42:24Z")

</div>

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:

![Capture.PNG](https://d3kjluh73b9h9o.cloudfront.net/original/4X/0/1/3/0139d261efb96ff7a61f95e36b00d9399298cb3c.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”.

---

<div class="post-metadata">

**Author:** ![Raildex](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Raildex](https://forums.unrealengine.com/u/Raildex)\
**Post date:** [July 28, 2016, 4:00pm UTC](https://forums.unrealengine.com/t/blueprint-bubblesort-doesnt-work-correctly/70040/6 "2016-07-28T16:00:21Z")

</div>

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

 ![Unbenannt.jpg](https://d3kjluh73b9h9o.cloudfront.net/original/3X/3/d/3d012ef7e257cefab6949af8d450b5bc80433a6b.jpeg)

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

---

<div class="post-metadata">

**Author:** ![Dementiurge](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Dementiurge](https://forums.unrealengine.com/u/Dementiurge)\
**Post date:** [July 28, 2016, 10:13pm UTC](https://forums.unrealengine.com/t/blueprint-bubblesort-doesnt-work-correctly/70040/7 "2016-07-28T22:13:08Z")

</div>

> [@Raildex\_;571616](#):
>
> ![Sort.png](https://d3kjluh73b9h9o.cloudfront.net/original/4X/d/9/4/d94911a9aadaeb894ab70523bffb6d9e2b17aa16.png)  
> This is the Sort

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.
