# How to sort a hit array by instance index?

**URL:** <https://forums.unrealengine.com/t/how-to-sort-a-hit-array-by-instance-index/468801>\
**Category:** Programming & Scripting\
**Tags:** unreal-engine\
**Created:** [July 28, 2020, 3:29pm UTC](https://forums.unrealengine.com/t/how-to-sort-a-hit-array-by-instance-index/468801 "2020-07-28T15:29:22Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![PerCat](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/percat/32/479573_2.png) [@PerCat](https://forums.unrealengine.com/u/PerCat)\
**Post date:** [July 28, 2020, 3:29pm UTC](https://forums.unrealengine.com/t/how-to-sort-a-hit-array-by-instance-index/468801/1 "2020-07-28T15:29:22Z")

</div>

Hey all I have a dungeon generator I’ve been working on for a while, it’s using hisms for everything and I’m attempting to simply brute force doorways spawning by getting random walls, sphere tracing them to check for any walls that are “good” and then there is a chance it get’s replaced by a door.

I’m using streams for everything that way a player can potentially have the same world from a seed they like or whatever. For some reason I have slight variance in door spawning locations on the same seeds. I figured out the multisphere trace seems to randomly sort it’s hit array.

I want to take the array and sort it by index but I have no idea how to sort an array by index. If anyone has some ideas on how to fix my problem that would be great.

I just need the hit array of instances to get sorted, preferably by index, or any, real, predictable way would be fine. My problem is multisphere trace doesn’t like to put hits in the array the same, even if the trace is in the exact same location.

Thanks!

---

<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:** [July 28, 2020, 3:48pm UTC](https://forums.unrealengine.com/t/how-to-sort-a-hit-array-by-instance-index/468801/2 "2020-07-28T15:48:21Z")

</div>

You can use min / max of array + While loop.

 ![308337-annotation-2020-07-28-164617.jpg](https://d3kjluh73b9h9o.cloudfront.net/original/4X/b/f/6/bf6a1b84e71d7f42610e352250240f2e3cf24949.jpeg)

The first loop creates a random array. While loop keeps picking min integer for as long as there anything in the unsorted left, adds it to a sorted array and removes it from unsorted. This prints the sorted from min to max value.

![308338-annotation-2020-07-28-165124.jpg](https://d3kjluh73b9h9o.cloudfront.net/original/4X/0/7/f/07f397de8e6ba871869dafafba072867ca39e6d0.jpeg)

---

<div class="post-metadata">

**Author:** ![PerCat](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/percat/32/479573_2.png) [@PerCat](https://forums.unrealengine.com/u/PerCat)\
**Post date:** [July 28, 2020, 3:55pm UTC](https://forums.unrealengine.com/t/how-to-sort-a-hit-array-by-instance-index/468801/3 "2020-07-28T15:55:53Z")

</div>

Do you know how I would go about getting my hit instances into that array sorted?

Edit: This was all I was able to get so far

 ![308340-sortarrayhelp.png](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/2/1/221eb96b4bce9f4fb5824a8fe418477c26fd18d2.png)

---

<div class="post-metadata">

**Author:** ![slonorib](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/slonorib/32/594422_2.png) [@slonorib](https://forums.unrealengine.com/u/slonorib)\
**Post date:** [July 28, 2020, 4:38pm UTC](https://forums.unrealengine.com/t/how-to-sort-a-hit-array-by-instance-index/468801/4 "2020-07-28T16:38:08Z")

</div>

Don’t sort arrays in blueprints: readability and performance of such implementations will be worse than C++ TArray. Also you can easily make mistakes.

Check TArray documentation 🙂 [TArray: Arrays in Unreal Engine | Unreal Engine Documentation](https://docs.unrealengine.com/en-US/Programming/UnrealArchitecture/TArrays/#sorting)

---

<div class="post-metadata">

**Author:** ![PerCat](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/percat/32/479573_2.png) [@PerCat](https://forums.unrealengine.com/u/PerCat)\
**Post date:** [July 28, 2020, 4:45pm UTC](https://forums.unrealengine.com/t/how-to-sort-a-hit-array-by-instance-index/468801/5 "2020-07-28T16:45:07Z")

</div>

I don’t know c and performance isn’t gonna be an issue, this is pre-gen type stuff that won’t effect gameplay

---

<div class="post-metadata">

**Author:** ![ClockworkOcean](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/clockworkocean/32/1169999_2.png) [@ClockworkOcean](https://forums.unrealengine.com/u/ClockworkOcean)\
**Post date:** [July 28, 2020, 5:11pm UTC](https://forums.unrealengine.com/t/how-to-sort-a-hit-array-by-instance-index/468801/6 "2020-07-28T17:11:01Z")

</div>

Write a functon to get the max index. The write another foreach which repeatedly finds the max and removes it, putting it in a second array.

---

<div class="post-metadata">

**Author:** ![PerCat](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/percat/32/479573_2.png) [@PerCat](https://forums.unrealengine.com/u/PerCat)\
**Post date:** [July 28, 2020, 5:46pm UTC](https://forums.unrealengine.com/t/how-to-sort-a-hit-array-by-instance-index/468801/7 "2020-07-28T17:46:58Z")

</div>

I posted a screenshot of what I got up above, I’m away from pc right now but my problem is re-adding the instance I need to a now sorted array.

---

<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:** [July 28, 2020, 5:59pm UTC](https://forums.unrealengine.com/t/how-to-sort-a-hit-array-by-instance-index/468801/8 "2020-07-28T17:59:44Z")

</div>

> but my problem is re-adding the  
> instance I need to a now sorted array

Your first problem is getting _the correct_ values into that very first _HISM Indexes_ array. Think about it. You hit _some_ instances with MultiSphere Trace but here you’re adding just 0,1,2,3 indexes of an array - these are _not_ HISM instance IDs.

Instance IDs come from the _Hit Item_ when you break the _Hit Result_ struct - and you probably already have it.

---

<div class="post-metadata">

**Author:** ![ClockworkOcean](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/clockworkocean/32/1169999_2.png) [@ClockworkOcean](https://forums.unrealengine.com/u/ClockworkOcean)\
**Post date:** [July 28, 2020, 6:09pm UTC](https://forums.unrealengine.com/t/how-to-sort-a-hit-array-by-instance-index/468801/9 "2020-07-28T18:09:12Z")

</div>

It’s like Everynone says, you need to put the right thing in the array first. Then make a MAX func ( or min ), then do:

 ![308356-sort.jpg](https://d3kjluh73b9h9o.cloudfront.net/original/4X/b/4/6/b46aab5c062ee6bb056645944540caad5f6d350e.jpeg)

---

<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:** [July 28, 2020, 6:46pm UTC](https://forums.unrealengine.com/t/how-to-sort-a-hit-array-by-instance-index/468801/10 "2020-07-28T18:46:36Z")

</div>

> Don’t sort arrays in blueprints:  
> readability and performance of such  
> implementations will be worse than C++  
> TArray. Also you can easily make  
> mistakes.

Also this. While true, sure - why reinvent the (really well oiled!) wheel, this answer feels like a generic stack overflow quote. An answer to ‘how to do it in **blueprints** ’ can never be ‘don’t do it in **blueprints** ’.

Don’t like blueprint spaghetti? Go eat the a, b, c++ alphabet soup. 😉

* * *

Also, the Extended Standard Library plugin has sorting afair.

---

<div class="post-metadata">

**Author:** ![ClockworkOcean](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/clockworkocean/32/1169999_2.png) [@ClockworkOcean](https://forums.unrealengine.com/u/ClockworkOcean)\
**Post date:** [July 28, 2020, 7:08pm UTC](https://forums.unrealengine.com/t/how-to-sort-a-hit-array-by-instance-index/468801/11 "2020-07-28T19:08:28Z")

</div>

It does, very efficiently. But I discovered it also wont package without a compiler… :-/

---

<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:** [July 28, 2020, 9:06pm UTC](https://forums.unrealengine.com/t/how-to-sort-a-hit-array-by-instance-index/468801/12 "2020-07-28T21:06:44Z")

</div>

Oh, that’s a bummer then.

---

<div class="post-metadata">

**Author:** ![PerCat](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/percat/32/479573_2.png) [@PerCat](https://forums.unrealengine.com/u/PerCat)\
**Post date:** [July 29, 2020, 5:55am UTC](https://forums.unrealengine.com/t/how-to-sort-a-hit-array-by-instance-index/468801/13 "2020-07-29T05:55:27Z")

</div>

Yup, yup. Got it! Thank you much man!

---

<div class="post-metadata">

**Author:** ![slonorib](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/slonorib/32/594422_2.png) [@slonorib](https://forums.unrealengine.com/u/slonorib)\
**Post date:** [July 29, 2020, 10:04am UTC](https://forums.unrealengine.com/t/how-to-sort-a-hit-array-by-instance-index/468801/14 "2020-07-29T10:04:32Z")

</div>

lol, I’ll just keep watching your blueprint experiments.  
“I don’t know C” is not the issue here. It’s better to get acquainted with it to avoid problems like this.  
Btw, Extendernd Standard Library won’t help you: you can’t specify sofrting function in it 🙂

---

<div class="post-metadata">

**Author:** ![ClockworkOcean](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/clockworkocean/32/1169999_2.png) [@ClockworkOcean](https://forums.unrealengine.com/u/ClockworkOcean)\
**Post date:** [July 29, 2020, 11:06am UTC](https://forums.unrealengine.com/t/how-to-sort-a-hit-array-by-instance-index/468801/15 "2020-07-29T11:06:44Z")

</div>

Oh yes you can… 😉
