Sort array by age

Hello,

I have noticed there are no “Sort” for both ascending and descending.

How would I go about adding a sorting method that would sort objects with a field value that has “age” in it, where the youngest is at the beginning of the list and the oldest is at the end of the list?

You need to define custom predicate struct with () operator override that will compare if right object is bigger then left object, Sort will use that function to sort it in right way, inversion of will compare will invert the sort order:

Array.Sort<FNameOfPredicateStruct>();

Here you got few examples how to use it in engine code:

https://github.com/EpicGames/UnrealEngine/blob/ab9713f2c0346236b0fdd271d3304cf0991d23de/Engine/Plugins/2D/Paper2D/Source/Paper2DEditor/Private/PaperFlipbookHelpers.cpp#L133
https://github.com/EpicGames/UnrealEngine/blob/ab9713f2c0346236b0fdd271d3304cf0991d23de/Engine/Source/Runtime/Online/OnlineSubsystemUtils/Private/PartyBeaconState.cpp#L224
https://github.com/EpicGames/UnrealEngine/blob/ab9713f2c0346236b0fdd271d3304cf0991d23de/Engine/Source/Developer/Merge/Private/SMergeGraphView.cpp#L107

Hey, thanks I know there is a way to sort in C++ code but I was asking about how to do it in Blueprint Scripting. That is why I posted it in Blueprint Scripting.

Then no, blueprint in current form does can’t support something like that, it would require custom K2Node

As said it is not possible without doing K2Node which is correct, except you can do it in blueprint with the twist of creating your own “MaxOfIntArray” or “MinOfIntArray” similar concept.

So what I did was created a function that did exactly the same except of outputting Integer as the Max or Min Value I outputted the object that is to be sorted, then from there I can loop through and rearrange it all. Which works, now this doesn’t work for really really big arrays as it can cause some delays on sorting so it is best that you keep the list as small as possible.

Can you send us a picture of your function ? :slight_smile: