Crovea

Untitled

Nov 2nd, 2023
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.97 KB | None | 0 0
  1. #include "RancorousSortingLibrary.h"
  2.  
  3. #include "ISortableElement.h"
  4.  
  5. void URancorousSortingLibrary::SortSortableArray(TArray<UObject*>& ArrayToSort)
  6. {
  7. ArrayToSort.Sort([](const UObject& A, const UObject& B) {
  8. if (const ISortableElement* SortableA = Cast<ISortableElement>(&A))
  9. {
  10. return SortableA->IsLessThan(&B);
  11. }
  12. return false;
  13. });
  14. }
  15.  
  16. TArray<UObject*> URancorousSortingLibrary::GetSortedArrayCopy(const TArray<UObject*>& ArrayToSort)
  17. {
  18. TArray<UObject*> SortedArray = ArrayToSort;
  19. SortedArray.Sort([](const UObject& A, const UObject& B) {
  20. if (const ISortableElement* SortableA = Cast<ISortableElement>(&A))
  21. {
  22. return SortableA->IsLessThan(&B);
  23. }
  24. return false;
  25. });
  26. return SortedArray;
  27. }
  28.  
  29.  
  30. TArray<UObject*> URancorousSortingLibrary::GetSortedArrayCopyWithDelegate(const TArray<UObject*>& ArrayToSort, const FCompareDelegate& ComparisonFunction)
  31. {
  32. TArray<UObject*> SortedArray = ArrayToSort;
  33. SortedArray.Sort([&](const UObject& A, const UObject& B) {
  34. return ComparisonFunction.Execute(&A, &B);
  35. });
  36. return SortedArray;
  37. }
  38.  
  39. ======= CPP FILE BELOW
  40.  
  41. #pragma once
  42.  
  43. #include "CoreMinimal.h"
  44. #include "Kismet/BlueprintFunctionLibrary.h"
  45. #include "RancorousSortingLibrary.generated.h"
  46.  
  47. DECLARE_DYNAMIC_DELEGATE_RetVal_TwoParams(bool, FCompareDelegate, const UObject*, ElementA, const UObject*, ElementB);
  48.  
  49. UCLASS()
  50. class RANCOROUSUTILITIES_API URancorousSortingLibrary : public UBlueprintFunctionLibrary
  51. {
  52. GENERATED_BODY()
  53.  
  54. public:
  55. UFUNCTION(BlueprintCallable, Category = "Sorting")
  56. static void SortSortableArray(UPARAM(ref) TArray<UObject*>& ArrayToSort);
  57.  
  58. UFUNCTION(BlueprintCallable, Category = "Sorting", BlueprintPure)
  59. static TArray<UObject*> GetSortedArrayCopy(const TArray<UObject*>& ArrayToSort);
  60.  
  61. UFUNCTION(BlueprintCallable, Category = "Sorting", BlueprintPure)
  62. static TArray<UObject*> GetSortedArrayCopyWithDelegate(const TArray<UObject*>& ArrayToSort, const FCompareDelegate& ComparisonFunction);
  63. };
Advertisement
Add Comment
Please, Sign In to add comment