How can I sort items in array by properties?

Great extension! I wasted a lot of time on my own mechanism. This plug-in should come with a default installation.

Here is another way to do it, my BP also stores the old Index if somebody needs that.

1 Like

Characterize a correlation capability: Begin by characterizing a capability that looks at two things in light of the ideal property. This capability will be utilized as a boundary for the arranging calculation.

Use the algorithm for sorting: Apply an arranging calculation to the cluster, passing the examination capability as a boundary. There are different arranging calculations accessible, for example, bubble sort, addition sort, blend sort and quicksort. The decision of calculation relies upon factors like the size of the exhibit and the ideal productivity.

Execute the correlation capability: The correlation capability ought to accept two things as info and return a worth demonstrating their relative request. On the off chance that the main thing ought to precede the subsequent thing, return a negative worth. On the off chance that the primary thing ought to come after the subsequent thing, return a positive worth. Assuming the things are viewed as equivalent, bring zero back. The particular examination rationale relies upon the property you need to sort by.

This helped me solve my issue, so thanks! I got a small “accessed none” errors from the Get when I copied your Blueprint exactly though, so I threw in some safety checks. Also wanted the function to be more flexible so I put in a select that’s controlled by an enum that’s set manually on the node, together with an option to switch direction of the sorting.

1 Like

The inability to sort an array by any property of a class in blueprint is extremely problematic. This thread has shown up multiple times in my search for a generic solution. I’m here to share my own solution to this problem.

TLDR, this solution will require you to have a cpp project (you can easily convert from blueprint only to a cpp project). You dont need to understand the logic if you’re not a programmer, just copy and paste it into your project and use only the blueprint portion.

The idea is that you’re using cpp underneath to do the sorting, but the way that mechanism is exposed in blueprint allows you to sort by any property. So what is the secret sauce? We just sidecar our actor and its value in a tuple, and let cpp do the sorting base on value. Now our actors are sorted accordingly.

Here is a screenshot of what it looks like in blueprint:

And here is the cpp code that you need.

#pragma once

#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include <tuple>
#include <vector>
#include <algorithm>
#include "TupleSorter.generated.h"

UCLASS(BlueprintType, Blueprintable)
class TRDECKBUILDER_API USortableTuple : public UObject
{
	GENERATED_BODY()

public:

	UPROPERTY(BlueprintReadWrite, EditInstanceOnly, Category="Default", meta=(ExposeOnSpawn="true"))
	TObjectPtr<AActor> Actor;
	
	UPROPERTY(BlueprintReadWrite, EditAnywhere, Category="Default", meta=(ExposeOnSpawn="true"))
	int32 Value;

};

UCLASS()
class TRDECKBUILDER_API UTupleSorter : public UObject
{
	GENERATED_BODY()

public:

	UFUNCTION(BlueprintCallable, Category = "Tuple Sorting")
	static void Sort(UPARAM(ref) TArray<USortableTuple*>& TupleList) {
		TupleList.Sort([](const USortableTuple& A, const USortableTuple& B){
			return A.Value < B.Value;
		});
	}
};

Additional notes, you can use this to sort basically most things. Just pass in a blueprint actor and some integer value that represent the order/priority of that actor. If you need to sort by floats, you can easily just convert those into an int (x100 or however many decimals you want to shift).

If you need to sort by strings, you’ll need to do a bit more work but can simply follow this same schematic. Hope this helps!