# How to sort TArray in c++?

**URL:** <https://forums.unrealengine.com/t/how-to-sort-tarray-in-c/465585>\
**Category:** C++\
**Tags:** tarray, sorting, unreal-engine, CPP, question\
**Created:** [May 21, 2020, 4:57pm UTC](https://forums.unrealengine.com/t/how-to-sort-tarray-in-c/465585 "2020-05-21T16:57:18Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![YanDaik](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@YanDaik](https://forums.unrealengine.com/u/YanDaik)\
**Post date:** [May 21, 2020, 4:57pm UTC](https://forums.unrealengine.com/t/how-to-sort-tarray-in-c/465585/1 "2020-05-21T16:57:18Z")

</div>

If i have an array of pointers:

```
TArray<OfClassPointers*> TheArray;

```

I cant do like this :

```
TheArray.Sort([](const OfClassPointers& ip1, const OfClassPointers& ip2) {
    			return ip1.level < ip2.level; // or ip1-> or even *ip->
    		});

```

Documentation says

> use Algo::Sort(MyArray, Predicate)  
> but i have no idea how.

Can anyone help?

---

<div class="post-metadata">

**Author:** ![JSwigart](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/jswigart/32/469203_2.png) [@JSwigart](https://forums.unrealengine.com/u/JSwigart)\
**Post date:** [May 21, 2020, 5:48pm UTC](https://forums.unrealengine.com/t/how-to-sort-tarray-in-c/465585/2 "2020-05-21T17:48:31Z")

</div>

The array stores pointers, so the arguments of your lambda function needs to be pointers, not references.

---

<div class="post-metadata">

**Author:** ![YanDaik](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@YanDaik](https://forums.unrealengine.com/u/YanDaik)\
**Post date:** [July 19, 2020, 3:18pm UTC](https://forums.unrealengine.com/t/how-to-sort-tarray-in-c/465585/3 "2020-07-19T15:18:04Z")

</div>

Have you tried it? Compiler gives compilation error.

---

<div class="post-metadata">

**Author:** ![knapeczadam](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@knapeczadam](https://forums.unrealengine.com/u/knapeczadam)\
**Post date:** [July 19, 2020, 7:51pm UTC](https://forums.unrealengine.com/t/how-to-sort-tarray-in-c/465585/4 "2020-07-19T19:51:07Z")

</div>

Algo::Sort(TheArray, [](const OfClassPointers\* LHS, const OfClassPointers\* RHS) { /\* … \*/ });

---

<div class="post-metadata">

**Author:** ![Steve\_Robb](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Steve\_Robb](https://forums.unrealengine.com/u/Steve_Robb)\
**Post date:** [November 6, 2020, 8:04pm UTC](https://forums.unrealengine.com/t/how-to-sort-tarray-in-c/465585/5 "2020-11-06T20:04:20Z")

</div>

Hi,

When you’re sorting by some property of your pointed-to type, it is simpler doing something like this:

```
Algo::SortBy(TheArray, &OfClassPointers::level);

```

… or this, if you want to sort in reverse:

```
Algo::SortBy(TheArray, &OfClassPointers::level, TGreater<>());

```

Saying that, your original code should have worked, so I’m not sure what problem you were having.

Steve

---

<div class="post-metadata">

**Author:** ![AnotherZach](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@AnotherZach](https://forums.unrealengine.com/u/AnotherZach)\
**Post date:** [November 9, 2020, 12:51am UTC](https://forums.unrealengine.com/t/how-to-sort-tarray-in-c/465585/6 "2020-11-09T00:51:30Z")

</div>

It is much easier to just call .Sort() on the variable holding the TArray. If you are using a custom data structure, be sure to overload the less than operator. Example of the syntax attached.

 ![321467-screen-shot-2020-11-08-at-64937-pm.png](https://d3kjluh73b9h9o.cloudfront.net/original/4X/a/9/8/a982ebcbb5dc53e5b3e3749c783e4e46a857dfb7.png)

---

<div class="post-metadata">

**Author:** ![King\_Crazy](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@King\_Crazy](https://forums.unrealengine.com/u/King_Crazy)\
**Post date:** [November 9, 2020, 2:08am UTC](https://forums.unrealengine.com/t/how-to-sort-tarray-in-c/465585/7 "2020-11-09T02:08:45Z")

</div>

The way I did it was I created a static “predicate” function which returns a bool: whether the item should be sorted or not.

If you wanted to have a function that sorts items of the type AMyClass, you’d declare a public static function with a bool return type, like the following example (Since it’s static, the class you declare this function is up to you. For the sake of this example, I’ll be declaring it inside an imaginary AMyClass class):

```
inline static bool SortPredicate(AMyClass* itemA, AMyClass* itemB);

```

Then, go to the respective \*.cpp file and define the predicate function. This way of handling a sort is especially useful if you want to sort based on a particular variable from the class you’re passing in. For instance, lets say we had a class representing our character that holds multiple stats like speed, strength, etc… we could make a predicate function to sort an array by the speed stat, so that higher speed stats appear at the front of the array. The function should return true if the sort should occur (I believe, at least – someone please correct me if I’m wrong).

```
inline bool AMyClass::SortPredicate(AMyClassitemA, AMyClassitemB)
{
    return (itemA->myVar > itemB->myVar);
}

```

If it seems to be sorting incorrectly, just flip the sign.  
Finally, you can pass the function when using the .Sort() method on the TArray.

```
myArray.Sort(AMyClass::SortPredicate);

```

Hope that helps! 🙂

---

<div class="post-metadata">

**Author:** ![JSwigart](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/jswigart/32/469203_2.png) [@JSwigart](https://forums.unrealengine.com/u/JSwigart)\
**Post date:** [November 11, 2020, 4:18pm UTC](https://forums.unrealengine.com/t/how-to-sort-tarray-in-c/465585/8 "2020-11-11T16:18:20Z")

</div>

Your example in your question compiles fine for me in MSVC 2019.

For example

```
TArray<USceneComponent*> TheArray;

TheArray.Sort([](const USceneComponent& ip1, const USceneComponent& ip2) {
	// access some random field just to test compile
	return ip1.IsDeferringMovementUpdates() < ip2.IsDeferringMovementUpdates();
});

```

What error did you get? Did you include the file for OfClassPointers so that it’s defined wherever you are trying to sort by it?

---

<div class="post-metadata">

**Author:** ![xsm909](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/xsm909/32/920189_2.png) [@xsm909](https://forums.unrealengine.com/u/xsm909)\
**Post date:** [December 26, 2022, 4:07pm UTC](https://forums.unrealengine.com/t/how-to-sort-tarray-in-c/465585/10 "2022-12-26T16:07:17Z")

</div>

> [@YanDaik](#):
>
> ```auto
> TheArray.Sort([](const OfClassPointers& ip1, const OfClassPointers& ip2) {
> return ip1.level < ip2.level; // or ip1-> or even *ip->
> });
> 
> ```

Storage.Sort([**&**](const FItem& item1, const FItem& item2) {return item1.p\> item2.p; });

Work properly

---

<div class="post-metadata">

**Author:** ![theseus2703](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@theseus2703](https://forums.unrealengine.com/u/theseus2703)\
**Post date:** [February 17, 2023, 1:30am UTC](https://forums.unrealengine.com/t/how-to-sort-tarray-in-c/465585/11 "2023-02-17T01:30:25Z")

</div>

I had a problem with that as well, when trying to sort a TSet\<AMyClass\*\> like this:

MySet.Sort([](const AMyClass& A, const AMyClass& B) {  
return A.calculateSomeScore() \> B.calculateSomeScore();  
});

it would not compile, unless i declared the function “calculateSomeScore()” like this:

int32 calculateSomeScore() **const** ; in the .h file and

int32 AMyClass::calculateSomeScore() **const**  
{ … }

in the .cpp file.

I hope this helps anyone with the same problem, and this answer was not given here.

---

<div class="post-metadata">

**Author:** ![Aubrey](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Aubrey](https://forums.unrealengine.com/u/Aubrey)\
**Post date:** [October 3, 2023, 5:16am UTC](https://forums.unrealengine.com/t/how-to-sort-tarray-in-c/465585/13 "2023-10-03T05:16:58Z")

</div>

I struggled a bit with this 2 and it only became clear after learning what predicates are.  
I also wrote some tutorials/guides on how to work with the data structures for future reference…

There is a section like below you’ll be interested in

" In this example we are passing them in by reference because the TArray::Sort() function REQUIRES your predicate to take in references even if your TArray is made up of pointers.

So TArray\<UApples\*\> will still require a predicate using UApples&…"

> **[UE5 Creating Predicates for TArray, TMap And TSets – Unreal C++ API](https://store.algosyntax.com/tutorials/unreal-engine/ue5-creating-predicates-for-tarray-tmap-and-tsets/)**
>
> While trying to sort a TArray in unreal engine you may come across a requirement to define a  PREDICATE\_CLASS& Predicate. In this tutorial, we will

> **[Sorting in Unreal Engine 5: A TArray and TMap Primer – Unreal C++ API](https://store.algosyntax.com/tutorials/unreal-engine/sorting-in-unreal-engine-5-a-tarray-and-tmap-primer/)**
>
> Sorting is an essential operation in computer programming that helps us organize data in a particular order, making it easier to search, analyze, and
