NTH algorithm for TArray?????

hi UE forumers, i try to get a better choice of sizes in my TArray from better to worst!, usually i implement std::nth_element() for this kind of task, but i dont know how to for TArrays without a lot of statements jaja,

any suggestion?

TArray’s closest thing to std::nth_element() is the Sort function. The only major difference, that I can remember, between std::nth_element() and a regular sort is that nth_element() can bail out early.

If you’d prefer to implement a function similar to std::nth_element(), it is pretty trivial to do so. I’m sure you know “what” nth_element() does, but maybe not how?

hey thanks zachary!, right now i am using this approach:

``````

int32 hold;
for (int pass = 1; pass < points.Num(); ++pass)
{
for (int i = 0; i < points.Num() - 1; ++i)
{
if (points* > points[i + 1])
{
hold = points*;
points* = points[i + 1];
points[i + 1] = hold;
}
}
}

``````

is working as expected, but my logic is very standard and basic hehe, i hope UE have own better way!, because i need use this for big amount of data every frame!.
I read all TArray documentantion without lucky, so i need re open my math books to undestand a fast approach, please tell me if you know a cool way!

hey! i found a way!, sort can be used as multipurpose by using predicates, i just need create a custom one!
check this out!

``````

//this is my predicate
struct Greater
{
inline bool operator()(const int32& pt1, const int32& pt2) const
{
return pt1> pt2;
}
};

//and this is my test function
TArray<int32> dontKnow;
for (int i = 0; i < 10; i++)
{
dontKnow.Add(i);
}

dontKnow.Sort(Greater());

for (int i = 0; i < dontKnow.Num(); i++)
{
GEngine->AddOnScreenDebugMessage(-1, 2, FColor::Green, FString::FromInt(dontKnow*));
}

``````

Now my TArray<int32> can be sort from greater to less, run as magic!!!