{ }Blueprint → C++

Unreal Engine 5 · Blueprint → C++

Sort an Array in Unreal Engine 5 C++UE Docs

Sorting a TArray in Unreal Engine 5 C++ uses TArray::Sort, which sorts ascending by default or accepts a lambda predicate for custom ordering.

Blueprint node & C++ equivalent

C++
Numbers.Sort();                                   // ascending
Numbers.Sort([](int32 A, int32 B){ return A > B; }); // custom

What does sorting an array do?

Sorting reorders the array's elements according to a comparison rule, most often ascending order. In C++ this is handled by member functions on TArray rather than a standalone node.

The C++ equivalent

Numbers.Sort() sorts in ascending order using the element type's operator<. To sort with a custom rule, pass a lambda: Numbers.Sort([](int32 A, int32 B){ return A > B; }) sorts descending. The lambda returns true when A should come before B.

Stable sorting and pointers

TArray::Sort is not guaranteed to be stable, meaning equal elements may change relative order. Use StableSort when preserving that order matters. Note that sorting an array of pointers with the default Sort compares the pointer addresses, so supply a predicate that dereferences them.

Frequently asked questions

How do you sort a TArray in UE5 C++?+

Call MyArray.Sort() for ascending order, or pass a lambda predicate like MyArray.Sort([](auto A, auto B){ return A > B; }) for custom ordering.

How do you sort a TArray in descending order?+

Pass a predicate that returns true when the first element should come first, for example Numbers.Sort([](int32 A, int32 B){ return A > B; }).

Related Arrays & Containers nodes

View all Arrays & Containers nodes →