# Combine to Variables / Values? Order an array?

**URL:** <https://forums.unrealengine.com/t/combine-to-variables-values-order-an-array/2129262>\
**Category:** Blueprint\
**Tags:** unreal-engine, question\
**Created:** [November 16, 2024, 11:06pm UTC](https://forums.unrealengine.com/t/combine-to-variables-values-order-an-array/2129262 "2024-11-16T23:06:11Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![NebulaCricket42](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@NebulaCricket42](https://forums.unrealengine.com/u/NebulaCricket42)\
**Post date:** [November 16, 2024, 11:06pm UTC](https://forums.unrealengine.com/t/combine-to-variables-values-order-an-array/2129262/1 "2024-11-16T23:06:11Z")

</div>

I’m trying to take the distance of all the elements in this array and then I want to order the list by closest to farthest.

I’m not sure how to compare the organize and compare the values ultimately.  
AI seems to recommend a structure (not familiar)  
Is it possible to just create a second array and compare the two Index’s of the array? (Idk maybe that won’t work)

 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/8/c/b/8cbe1fc3f18afb962cf33ca5cd646a60917d49e8.png)

---

<div class="post-metadata">

**Author:** ![silnarm](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/silnarm/32/955303_2.png) [@silnarm](https://forums.unrealengine.com/u/silnarm)\
**Post date:** [November 17, 2024, 3:39am UTC](https://forums.unrealengine.com/t/combine-to-variables-values-order-an-array/2129262/2 "2024-11-17T03:39:59Z")

</div>

Structs let you combine some variables into one ‘structure’, and while this is how would you would solve the problem in a regular programming language, BP does not support sorting them.

Things like this are no fun in BP, but of course anything is possible…  
The second array idea can work, build up a distance array as you build your result array, insert into the distance array at the appropriate place (loop and find index to insert at) and then insert into the result array at the same index.

I used actors here, I see now you are using components, so some minor changes required:

Helper function to insert into the distance array and return the index:

 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/d/6/7/d67bca74dbcc0e21c7ecc94a4cb14d561b76a317.png)

“Proximity Sort”:

 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/c/7/3/c73b2314f5d8b15b99845c1237fb53c4b14b7ad3.png)

`Distances` and `Result List` are local variables.

---

<div class="post-metadata">

**Author:** ![Everynone](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/everynone/32/1135017_2.png) [@Everynone](https://forums.unrealengine.com/u/Everynone)\
**Post date:** [November 17, 2024, 8:48am UTC](https://forums.unrealengine.com/t/combine-to-variables-values-order-an-array/2129262/3 "2024-11-17T08:48:57Z")

</div>

I’ll just leave it here because it’s awesome and related:

> [@Sorting algorithms in Blueprints](https://forums.unrealengine.com/t/sorting-algorithms-in-blueprints/1135992):
>
> It is common knowledge that blueprints do not include sorting functions. In my previous experiences, I have been able to create simple sorting functions easily. However, during my current project, I faced the challenge of sorting large data arrays and found that the sorting process was either very slow or unsuccessful. Since I wanted to avoid using C++ code or third-party development, I independently researched this issue. After conducting some research, I have decided to share my findings in t…

---

<div class="post-metadata">

**Author:** ![user\_473504a1534af3b6e5e5991bf13750e943fdd811b6fe7fb2bb234a](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@user\_473504a1534af3b6e5e5991bf13750e943fdd811b6fe7fb2bb234a](https://forums.unrealengine.com/u/user_473504a1534af3b6e5e5991bf13750e943fdd811b6fe7fb2bb234a)\
**Post date:** [November 17, 2024, 9:00am UTC](https://forums.unrealengine.com/t/combine-to-variables-values-order-an-array/2129262/4 "2024-11-17T09:00:42Z")

</div>

If you want to sort array elements in Blueprints, Unreal Engine doesn’t provide a default node for this. However, you can create a macro library in Blueprints and implement a sorting node within it. While it may not be as efficient as sorting in C++, it’s a feasible approach. Here’s my macro for bubble sort:

 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/1/b/7/1b7dff4bbca9e3493878f6384443c3b49803b88c.png)  
You can try creating a Blueprint Macro Library and replicate this logic if you don’t want to code it yourself. Alternatively, there are likely plugins available that offer similar functionality.
