当前位置:   article > 正文

UE4 C++ 对结构体数组内元素进行排序_ue4 数组排序

ue4 数组排序

对结构体数组排序需要两个必要条件,一是结构体定义内重写<操作符,二是元素内必须要有可以用来排序的属性例如int、float类型的变量

案例:

  1. .h
  2. UENUM(BlueprintType)
  3. enum class EOrient : uint8
  4. {
  5. North,
  6. East,
  7. South,
  8. West,
  9. };
  10. USTRUCT(BlueprintType)
  11. struct FATestActor
  12. {
  13. GENERATED_USTRUCT_BODY()
  14. public:
  15. UPROPERTY(EditAnyWhere, BlueprintReadWrite)
  16. FString str;
  17. UPROPERTY(EditAnyWhere, BlueprintReadWrite)
  18. EOrient orient;
  19. UPROPERTY(EditAnyWhere, BlueprintReadWrite)
  20. float a;
  21. bool operator<(const FATestActor& ATestActor) const
  22. {
  23. //>是从大到小排序,<就是从小到大
  24. return a > ATestActor.a;
  25. }
  26. };
  27. public:
  28. UFUNCTION(BlueprintCallable)
  29. void SortStruct(const TArray<FATestActor>& ArrayStruct);
  30. public:
  31. UPROPERTY(EditAnyWhere, BlueprintReadWrite)
  32. TArray<FATestActor> testArrayStruct;
  1. .cpp
  2. void ATestActors::SortStruct(const TArray<FATestActor>& ArrayStruct)
  3. {
  4. //const_cast<>()去掉const修饰符才能对数组进行更改
  5. const_cast<TArray<FATestActor>&>(ArrayStruct).Sort();
  6. }

本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号