RealDocs

TArray::Sort

function Core Since unknown
#include "Containers/Array.h"
Access: public

Description

Sorts the array in-place using an introspective sort (introsort). A no-argument overload sorts using operator<. Not guaranteed to preserve the relative order of equal elements.

Signature

void Sort(const PREDICATE_CLASS& Predicate)

Parameters

Name Type Description Default
Predicate PREDICATE_CLASS A callable implementing a strict weak ordering: returns true if A should come before B.

Return Type

void

Caveats & Gotchas

  • Not stable — equal elements may be reordered. Use StableSort() if relative order of equal elements must be preserved (e.g. sorting UI rows that were already in display order).
  • The predicate must implement strict weak ordering: for equal elements it must return false (not true). Returning true for equal elements causes undefined behaviour.
  • Sorts the entire array in-place. There is no range overload — to sort a subrange, extract, sort, and re-insert.

Example

Sort actors by distance and items by score C++
FVector PlayerPos = GetActorLocation();

// Sort closest-first
NearbyActors.Sort([&PlayerPos](const AActor* A, const AActor* B)
{
	return FVector::DistSquared(A->GetActorLocation(), PlayerPos)
		 < FVector::DistSquared(B->GetActorLocation(), PlayerPos);
});

// Sort descending by score (no predicate overload uses operator<)
Scores.Sort(); // ascending
// For descending, provide predicate:
Scores.Sort([](int32 A, int32 B) { return A > B; });

Version History

Introduced in: unknown

Version Status Notes
5.6 stable

Feedback

Was this helpful?

Suggest an edit

Select a field above to begin editing.