RealDocs

TArray::Heapify

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

Description

Rearranges the array in-place into a valid max-heap ordered by operator< (min value at the root). The predicate overload `Heapify(const PREDICATE_CLASS& Predicate)` accepts a custom comparator. Once heapified, use HeapPush, HeapPop, and HeapTop to maintain the invariant.

Caveats & Gotchas

  • Raw pointer elements are automatically dereferenced through TDereferenceWrapper — the comparator receives references to the pointed-to objects, not the pointers themselves. This does NOT apply to TSharedPtr or TWeakObjectPtr; those are compared by pointer value unless you supply a custom predicate.
  • Heapify does not sort the array — it only guarantees the heap invariant (parent >= children). If you need a fully sorted array, use Algo::Sort or TArray::Sort instead.

Signature

void Heapify()

Return Type

void

Example

Build a min-heap of task priorities C++
TArray<int32> Priorities = { 5, 3, 8, 1, 2 };
Priorities.Heapify(); // establishes heap property; Priorities[0] is now the largest

// Custom predicate to heapify by a struct field (min-heap by Cost):
TArray<FTask> Tasks;
Tasks.Heapify([](const FTask& A, const FTask& B){ return A.Cost < B.Cost; });

Version History

Introduced in: 4.0

Version Status Notes
5.6 stable

Feedback

Was this helpful?

Suggest an edit

Select a field above to begin editing.