对结构体数组排序需要两个必要条件,一是结构体定义内重写<操作符,二是元素内必须要有可以用来排序的属性例如int、float类型的变量
案例:
.h
UENUM(BlueprintType)
enum class EOrient : uint8
{
North,
East,
South,
West,
};
USTRUCT(BlueprintType)
struct FATestActor
{
GENERATED_USTRUCT_BODY()
public:
UPROPERTY(EditAnyWhere, BlueprintReadWrite)
FString str;
UPROPERTY(EditAnyWhere, BlueprintReadWrite)
EOrient orient;
UPROPERTY(EditAnyWhere, BlueprintReadWrite)
float a;
bool operator<(const FATestActor& ATestActor) const
{
//>是从大到小排序,<就是从小到大
return a > ATestActor.a;
}
};
public:
UFUNCTION(BlueprintCallable)
void SortStruct(const TArray<FATestActor>& ArrayStruct);
public:
UPROPERTY(EditAnyWhere, BlueprintReadWrite)
TArray<FATestActor> testArrayStruct;
.cpp
void ATestActors::SortStruct(const TArray<FATestActor>& ArrayStruct)
{
//const_cast<>()去掉const修饰符才能对数组进行更改
const_cast<TArray<FATestActor>&>(ArrayStruct).Sort();
}
|