[Question] How to sort array?

How to sort array using Sort, or Array.Sort(predicate) ?

I have defined Predicate like this:

bool ARPGItem::ConstituionPredicate(const ARPGItem& ip1, const ARPGItem& ip2)
{
	return ip1.Attributes.Constitution < ip2.Attributes.Constitution;
}

And now I’m trying to use it with sort:

Sort(EquipedItems[0], EquipedItems.Num(), &ARPGItem::ConstituionPredicate); 

Or with Array:

EquipedItems.Sort(&ARPGItem::ConstituionPredicate);

Array hold objects of type ARPGItem.

And I get big error:

1>          D:\Unreal\Rocket\Engine\Source\Runtime\Core\Public\Templates\Sorting.h(58) : while compiling class template member function 'bool TDereferenceWrapper::operator ()(ARPGItem *,ARPGItem *) const'
1>          with
1>          [
1>              T=ARPGItem *,
1>              PREDICATE_CLASS=bool (__cdecl ARPGItem::* )(const ARPGItem &,const ARPGItem &)
1>          ]
1>          D:\Unreal\Rocket\Engine\Source\Runtime\Core\Public\Templates\Sorting.h(101) : see reference to function template instantiation 'bool TDereferenceWrapper::operator ()(ARPGItem *,ARPGItem *) const' being compiled
1>          with
1>          [
1>              T=ARPGItem *,
1>              PREDICATE_CLASS=bool (__cdecl ARPGItem::* )(const ARPGItem &,const ARPGItem &)
1>          ]
1>          D:\Unreal\Rocket\Engine\Source\Runtime\Core\Public\Templates\Sorting.h(185) : see reference to class template instantiation 'TDereferenceWrapper' being compiled
1>          with
1>          [
1>              T=ARPGItem *,
1>              PREDICATE_CLASS=bool (__cdecl ARPGItem::* )(const ARPGItem &,const ARPGItem &)
1>          ]
1>          D:\Unreal\Rocket\Engine\Source\Runtime\Core\Public\Containers\Array.h(1419) : see reference to function template instantiation 'void Sort(T **,const int32,const PREDICATE_CLASS &)' being compiled
1>          with
1>          [
1>              PREDICATE_CLASS=bool (__cdecl ARPGItem::* )(const ARPGItem &,const ARPGItem &),
1>              T=ARPGItem
1>          ]
1>          D:\Unreal\RPG\Source\RPG\Components\RPGEquipmentManagerComponent.cpp(148) : see reference to function template instantiation 'void TArray::Sort(const PREDICATE_CLASS &)' being compiled
1>          with
1>          [
1>              InElementType=ARPGItem *,
1>              PREDICATE_CLASS=bool (__cdecl ARPGItem::* )(const ARPGItem &,const ARPGItem &)
1>          ]
1>          D:\Unreal\RPG\Source\RPG\Components\RPGEquipmentManagerComponent.cpp(148) : see reference to function template instantiation 'void TArray::Sort(const PREDICATE_CLASS &)' being compiled
1>          with
1>          [
1>              InElementType=ARPGItem *,
1>              PREDICATE_CLASS=bool (__cdecl ARPGItem::* )(const ARPGItem &,const ARPGItem &)
1>          ]

I’ve been looking trough Sorting.h and Array.h, but there is nothing about how predicate should be declared, where it should be defined, or how it should be used…
So I assumed it is used in similar manner to predicate for std::vector, but seems it is not the case.

Uhh I think I got working. Stackoverflow to the rescue!

Third answer.

And here is how you define predicate:

inline static bool ConstPredicate (const ARPGItem& ip1, const ARPGItem& ip2) 
{
	return (ip1.Attributes.Constitution > ip2.Attributes.Constitution);
}

Define it in header file of class you will be using your predicate.

And now sorting:

EquipedItems.Sort(URPGEquipmentManagerComponent::ConstPredicate);

Hi Lukasz,

It looks like your predicate function is a non-static member function. It should be made either static or a non-member (or use a lambda). A member function pointer isn’t a valid predicate.

Steve

I know this question was answered a long time ago, but you can also sort with a lambda expression.

EquipedItems.Sort([](const ARPGItem& ip1, const ARPGItem& ip2) { 
    return ip1.Attributes.Constitution > ip2.Attributes.Constitution;
});