Best way to sort an array of FHitResult by distance?

For SweepMultiByChannel should I be using FHitResult.Distance here? or do I need to get the actor first and get the distance from trace start to actor location? Documentation is a bit unclear to me what Distance will actually contain. So maybe something like the below?

TraceHitResults.Sort([]( const FHitResult& LTraceHitResult, const FHitResult& RTraceHitResult ) {
	return LTraceHitResult.Distance < RTraceHitResult.Distance;
});

I’m basically needing to sort from closest to furthest from trace start. I’m making a trace based projectile bullet manager and need to process closest hit first, which may result in subsequent hits being ignored depending on projectiles features.

In the documentation it states that OutHits results of SweepMultiByChannel are already sorted.

It just says the blocking hit will be sorted and it doesn’t say by what metric it’s sorted. I need all the results, regardless of blocking or not, sorted based off distance. The wording seams to imply it will place all overlaps first then first blocking hit as the last element.

The Distance property of the FHitResult struct in Unreal Engine represents the distance from the TraceStart to the Location of the hit in world space. It is a single-precision float value.

If you want to sort the FHitResult structs by distance from the TraceStart, you can use the Distance property directly in your sorting function. Here’s an example of how you can do this:

TraceHitResults.Sort([]( const FHitResult& LTraceHitResult, const FHitResult& RTraceHitResult ) {
    return LTraceHitResult.Distance < RTraceHitResult.Distance;
});

This will sort the TraceHitResults array in ascending order based on the Distance property of each FHitResult struct. The first element in the array will be the closest hit to the TraceStart, and subsequent elements will be sorted further away from the TraceStart.

If you need to get the distance from the TraceStart to the Location of an actor first, you can calculate it using the Distance property of the FHitResult struct. Here’s an example of how you can do this:

float DistanceToActor = TraceHitResults[0].Distance;

This will assign the distance from the TraceStart to the Location of the first hit in the TraceHitResults array to the DistanceToActor variable. You can then use this variable to determine which hit is the closest to the TraceStart.

hope it works!

Just basically just ChatGPT’d my own topic back to me, lol. But yes this works. I was looking for more clarification from people who actually use and write C++ though and have experience sorting FHitResult arrays. Will leave it at that I guess.