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

**URL:** <https://forums.unrealengine.com/t/looking-for-a-c-function-for-sorting-an-array-of-floats-and-returning-the-sorted-results-index/2236214>\
**Category:** C++\
**Tags:** unreal-engine, question\
**Created:** [December 22, 2024, 12:43am UTC](https://forums.unrealengine.com/t/looking-for-a-c-function-for-sorting-an-array-of-floats-and-returning-the-sorted-results-index/2236214 "2024-12-22T00:43:18Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![voidopolis](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@voidopolis](https://forums.unrealengine.com/u/voidopolis)\
**Post date:** [December 22, 2024, 12:43am UTC](https://forums.unrealengine.com/t/looking-for-a-c-function-for-sorting-an-array-of-floats-and-returning-the-sorted-results-index/2236214/1 "2024-12-22T00:43:18Z")

</div>

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.

 ![SortFunction-Overview](https://d3kjluh73b9h9o.cloudfront.net/original/4X/5/2/9/52983cbb4424e101eb146d93833b3ecb87e99fe5.png)  
 ![sortfunction1](https://d3kjluh73b9h9o.cloudfront.net/original/4X/0/d/6/0d6d8951b24bae085a9108412efa3937a0e79404.png)  
 ![SortFunction2](https://d3kjluh73b9h9o.cloudfront.net/original/4X/a/5/9/a59b753f46772d3316f59c6db6f5e495ea48ec3a.png)  
 ![SortFunction3](https://d3kjluh73b9h9o.cloudfront.net/original/4X/0/2/d/02d35fcd5c145d001df3e8a6e878038fea40a3be.png)  
 ![SortFunction4](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/0/3/2030da811c268470dc99e736ef475a54beeffb56.png)

---

<div class="post-metadata">

**Author:** ![tootzoe1](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@tootzoe1](https://forums.unrealengine.com/u/tootzoe1)\
**Post date:** [December 22, 2024, 1:49pm UTC](https://forums.unrealengine.com/t/looking-for-a-c-function-for-sorting-an-array-of-floats-and-returning-the-sorted-results-index/2236214/2 "2024-12-22T13:49:21Z")

</div>

you can try to sort the array by bubble sort…

.h file

```auto

#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:

```auto

#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;
            }
        }
    }
}

```

 ![Screenshot 2024-12-22 234225](https://d3kjluh73b9h9o.cloudfront.net/original/4X/5/b/7/5b7c19dafde7e37433a0b477392a8ee001618456.png)

---

<div class="post-metadata">

**Author:** ![voidopolis](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@voidopolis](https://forums.unrealengine.com/u/voidopolis)\
**Post date:** [December 22, 2024, 8:05pm UTC](https://forums.unrealengine.com/t/looking-for-a-c-function-for-sorting-an-array-of-floats-and-returning-the-sorted-results-index/2236214/3 "2024-12-22T20:05:18Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![system](https://d3kjluh73b9h9o.cloudfront.net/original/4X/7/1/3/713c9d3f58553f0de89543d76a8a3a2779dc9fa4.png) [@system](https://forums.unrealengine.com/u/system)\
**Post date:** [January 21, 2025, 8:05pm UTC](https://forums.unrealengine.com/t/looking-for-a-c-function-for-sorting-an-array-of-floats-and-returning-the-sorted-results-index/2236214/4 "2025-01-21T20:05:45Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
