Looking for a c++ function for sorting an array of floats and returning the sorted results + index

Looking for some C++ help here. I’m working on a racing game. I record the cars position by getting their distance along a spline track - which is represented as a float. A race would be made up of about 15-20 cars. The cars positions are stored in an array of floats. So, naturally I need to sort the array of floats so that I can put them on the score board in their proper places. I have a blueprint sort routine that works somewhat - but I think there is some kind of memory leak or some issue with the routine that I have not figured out. I’m also thinking that because distance values start to increase by quite a bit as you continue as you increase lap counts NOTE: Calculation: a racers position = (total spline distance + the racers current position along the spline ) * the number of laps the player has done. so the racers position pretty quickly get into the tans / hundreds of millions.

So I need a sort routine that inputs a single array of floats and the outputs two arrays: (1) the sorted floats (from largest to smallest) and (2) the index of the order of the sorted array. Basically, if I had 3 cars racing and their distances were [0] 200, [1] 100, [2] 300 and I sorted them I would end up with array 1 = [300,200,100] and the second array with [2, 0, 1]. I’m not a c++ programmer (just blueprint). I’ve used some AI engines to generate some code that should work, but I have not been able ot figure out how to compile it into a solution. So hoping that someone can craft a quick sort routine function library that I could load into my project.

below is the blueprint sort routine that I’m using currently.





you can try to sort the array by bubble sort…

.h file


#pragma once

#include "CoreMinimal.h"
#include "UObject/NoExportTypes.h"
#include "MyBubbleSort.generated.h"

UCLASS(Blueprintable)
class YOUR-PROJECT_API UMyBubbleSort  : public UObject
{
    GENERATED_BODY()

public:

    UFUNCTION(BlueprintCallable, Category = "TOOT")
    void BubbleSortFloatArr(const TArray<float> SrcArr, TArray<float>& ResultArr , TArray<int32>& ResultIdxArr);
};

.cpp file:


#include  "MyBubbleSort.h"

void UMyBubbleSort::BubbleSortFloatArr(const TArray<float> SrcArr, TArray<float> & ResultArr, TArray<int32> & ResultIdxArr)
{
    if(SrcArr.IsEmpty()) return;
    //
    int srcLen = SrcArr.Num()  ;
      ResultArr.Init(0.0, srcLen );
      ResultIdxArr.Init(0, srcLen );
      float* valPtr = ResultArr.GetData();
      int32* idxPtr = ResultIdxArr.GetData();
      //
    for (int i = 0 ; i < srcLen; ++i) {
        *(valPtr + i) = SrcArr[i] ;
        *(idxPtr + i) = i;
    }

    if (srcLen < 2) {
        return;
    }

    for (int i = 0 ; i < srcLen   ;  i++) {
        for (int j = 0 ; j < srcLen - 1 ;  j++) {
            if( *(valPtr + j + 1) > *(valPtr + j  )){
                 float tmpV =  *(valPtr + j  );
                 *(valPtr + j  ) = *(valPtr + j + 1);
                 *(valPtr + j + 1) = tmpV;
                 int32 tmpV2 = *(idxPtr + j  );
                 *(idxPtr + j  ) = *(idxPtr + j + 1);
                 *(idxPtr + j + 1) = tmpV2;
            }
        }
    }
}

Thank you very much for the bubble sort and the clear example of how to implement it. I was able to use JetBeans to implement the code and enable this in my blueprint.