How can i sort a TArray<const Type*> ?

when i sort a TArray<Type*> ,i use Array.sort((const Type* A, const Type* B){A->value < B->value}), but when sort TArray<const Type*> ,i have no idea to do this, i try many different ways to do it , always get compile err

Pointers are automatically de-referenced by the sort functions

TArray<const FData*> Arr;
Arr.Sort([](const FData& A, const FData& B) { return A.Val < B.Val; });

If you need to handle possible null pointers in the array, use Algo::Sort instead, to avoid de-referencing

Algo::Sort(Arr, [](const FData* A, const FData* B) {
    return (B == nullptr) || (A != nullptr && A->Val < B->Val);
});

thanks a lot, it’s very helpful

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.