# Sort array by age

**URL:** <https://forums.unrealengine.com/t/sort-array-by-age/342845>\
**Category:** Programming & Scripting\
**Tags:** unreal-engine\
**Created:** [January 1, 2016, 11:22am UTC](https://forums.unrealengine.com/t/sort-array-by-age/342845 "2016-01-01T11:22:27Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![anonymous\_user\_b346038e](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_b346038e](https://forums.unrealengine.com/u/anonymous_user_b346038e)\
**Post date:** [January 1, 2016, 11:22am UTC](https://forums.unrealengine.com/t/sort-array-by-age/342845/1 "2016-01-01T11:22:27Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Shadowriver](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Shadowriver](https://forums.unrealengine.com/u/Shadowriver)\
**Post date:** [January 1, 2016, 5:16pm UTC](https://forums.unrealengine.com/t/sort-array-by-age/342845/2 "2016-01-01T17:16:23Z")

</div>

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:

> **[TArray::Sort](https://docs.unrealengine.com/5.1/en-US/API/Runtime/Core/Containers/TArray/Sort/2/)**
>
> Sorts the array using user define predicate class.

```
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/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/Runtime/Online/OnlineSubsystemUtils/Private/PartyBeaconState.cpp#L224)  
[https://github.com/EpicGames/UnrealEngine/blob/ab9713f2c0346236b0fdd271d3304cf0991d23de/Engine/Source/Developer/Merge/Private/SMergeGraphView.cpp#L107](https://github.com/EpicGames/UnrealEngine/blob/ab9713f2c0346236b0fdd271d3304cf0991d23de/Engine/Source/Developer/Merge/Private/SMergeGraphView.cpp#L107)

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_b346038e](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_b346038e](https://forums.unrealengine.com/u/anonymous_user_b346038e)\
**Post date:** [January 1, 2016, 6:57pm UTC](https://forums.unrealengine.com/t/sort-array-by-age/342845/3 "2016-01-01T18:57:00Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Shadowriver](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Shadowriver](https://forums.unrealengine.com/u/Shadowriver)\
**Post date:** [January 1, 2016, 10:51pm UTC](https://forums.unrealengine.com/t/sort-array-by-age/342845/4 "2016-01-01T22:51:20Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_b346038e](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_b346038e](https://forums.unrealengine.com/u/anonymous_user_b346038e)\
**Post date:** [January 2, 2016, 5:52am UTC](https://forums.unrealengine.com/t/sort-array-by-age/342845/5 "2016-01-02T05:52:52Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_4f09631b1](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_4f09631b1](https://forums.unrealengine.com/u/anonymous_user_4f09631b1)\
**Post date:** [February 17, 2016, 10:56pm UTC](https://forums.unrealengine.com/t/sort-array-by-age/342845/6 "2016-02-17T22:56:58Z")

</div>

Can you send us a picture of your function ? 🙂
