Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "RancorousSortingLibrary.h"
- #include "ISortableElement.h"
- void URancorousSortingLibrary::SortSortableArray(TArray<UObject*>& ArrayToSort)
- {
- ArrayToSort.Sort([](const UObject& A, const UObject& B) {
- if (const ISortableElement* SortableA = Cast<ISortableElement>(&A))
- {
- return SortableA->IsLessThan(&B);
- }
- return false;
- });
- }
- TArray<UObject*> URancorousSortingLibrary::GetSortedArrayCopy(const TArray<UObject*>& ArrayToSort)
- {
- TArray<UObject*> SortedArray = ArrayToSort;
- SortedArray.Sort([](const UObject& A, const UObject& B) {
- if (const ISortableElement* SortableA = Cast<ISortableElement>(&A))
- {
- return SortableA->IsLessThan(&B);
- }
- return false;
- });
- return SortedArray;
- }
- TArray<UObject*> URancorousSortingLibrary::GetSortedArrayCopyWithDelegate(const TArray<UObject*>& ArrayToSort, const FCompareDelegate& ComparisonFunction)
- {
- TArray<UObject*> SortedArray = ArrayToSort;
- SortedArray.Sort([&](const UObject& A, const UObject& B) {
- return ComparisonFunction.Execute(&A, &B);
- });
- return SortedArray;
- }
- ======= CPP FILE BELOW
- #pragma once
- #include "CoreMinimal.h"
- #include "Kismet/BlueprintFunctionLibrary.h"
- #include "RancorousSortingLibrary.generated.h"
- DECLARE_DYNAMIC_DELEGATE_RetVal_TwoParams(bool, FCompareDelegate, const UObject*, ElementA, const UObject*, ElementB);
- UCLASS()
- class RANCOROUSUTILITIES_API URancorousSortingLibrary : public UBlueprintFunctionLibrary
- {
- GENERATED_BODY()
- public:
- UFUNCTION(BlueprintCallable, Category = "Sorting")
- static void SortSortableArray(UPARAM(ref) TArray<UObject*>& ArrayToSort);
- UFUNCTION(BlueprintCallable, Category = "Sorting", BlueprintPure)
- static TArray<UObject*> GetSortedArrayCopy(const TArray<UObject*>& ArrayToSort);
- UFUNCTION(BlueprintCallable, Category = "Sorting", BlueprintPure)
- static TArray<UObject*> GetSortedArrayCopyWithDelegate(const TArray<UObject*>& ArrayToSort, const FCompareDelegate& ComparisonFunction);
- };
Advertisement
Add Comment
Please, Sign In to add comment