# How to sort by newest through random items from an array?

**URL:** <https://forums.unrealengine.com/t/how-to-sort-by-newest-through-random-items-from-an-array/273640>\
**Category:** Blueprint\
**Tags:** unreal-engine, UE4-27, question\
**Created:** [January 20, 2022, 3:47am UTC](https://forums.unrealengine.com/t/how-to-sort-by-newest-through-random-items-from-an-array/273640 "2022-01-20T03:47:00Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![fael097](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/fael097/32/250778_2.png) [@fael097](https://forums.unrealengine.com/u/fael097)\
**Post date:** [January 20, 2022, 3:47am UTC](https://forums.unrealengine.com/t/how-to-sort-by-newest-through-random-items-from-an-array/273640/1 "2022-01-20T03:47:00Z")

</div>

I’m (still) making a multi-slot save/load game menu, and currently it kinda looks like this, Game Instance checks if slots named from 1 to 99 exist and add the ones that do exist into an array:

 ![Screenshot 2022-01-20 001009](https://d3kjluh73b9h9o.cloudfront.net/original/3X/e/e/eef1b3bb54a99fd38e1ce610e4c6cd410ba4f123.png)

then load screen widget loops through all items from that array and adds a single slot widget as a child to a scrollbox, assigning the Array Element number as an ID to each slot, so they all carry the same number as an existing save slot as ID.

 ![Screenshot 2022-01-20 001340](https://d3kjluh73b9h9o.cloudfront.net/original/3X/1/c/1ca0fb5e60acc4c299df9957b728584a37975c0c.jpeg)

you can later click those slots to set their ID as the active save slot so you can manipulate them, and do thing such as load from them, save into them or delete them.

now, currently they are sorted from oldest to newest (opposite to what I want) but that doesn’t even matter since when I overwrite one of those saves in the middle, they’re now the most recent save and should be on top of the list, except their ID can’t change so they stay at the same position.

So I suppose each Save Game slot should also keep stored an absolute incremental value to keep track of which order they were last saved, but I can’t imagine how I could do that or how would I loop through a second variable from those save slots and sort my list from those.

conveniently all saves store a DateTime of the time they were saved, but I’m also not sure if that could be used. ideas?

---

<div class="post-metadata">

**Author:** ![ClockworkOcean](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/clockworkocean/32/1169999_2.png) [@ClockworkOcean](https://forums.unrealengine.com/u/ClockworkOcean)\
**Post date:** [January 20, 2022, 8:14am UTC](https://forums.unrealengine.com/t/how-to-sort-by-newest-through-random-items-from-an-array/273640/2 "2022-01-20T08:14:58Z")

</div>

What about saving some form of date and time in the save file name?

Only thing is, when you update one, you have to delete it and make a new save game for it.

---

<div class="post-metadata">

**Author:** ![fael097](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/fael097/32/250778_2.png) [@fael097](https://forums.unrealengine.com/u/fael097)\
**Post date:** [January 22, 2022, 2:11am UTC](https://forums.unrealengine.com/t/how-to-sort-by-newest-through-random-items-from-an-array/273640/3 "2022-01-22T02:11:23Z")

</div>

I already do that

> conveniently all saves store a DateTime of the time they were saved, but I’m also not sure if that could be used. ideas?

I could add any kind of data to the save game BP that I want, I suppose I could add an ever incrementing integer if that would help.

I’m just not sure how I can use it to sort the slots which are spawned using values from a different array.

---

<div class="post-metadata">

**Author:** ![fael097](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/fael097/32/250778_2.png) [@fael097](https://forums.unrealengine.com/u/fael097)\
**Post date:** [January 22, 2022, 2:44am UTC](https://forums.unrealengine.com/t/how-to-sort-by-newest-through-random-items-from-an-array/273640/4 "2022-01-22T02:44:03Z")

</div>

So I was thinking, this is how it is currently:

 ![Screenshot 2022-01-21 232941](https://d3kjluh73b9h9o.cloudfront.net/original/3X/0/b/0b7441b15c49a5d02878033c9d27d2b29ff57b7c.jpeg)

Instead, I could get the saved date from each slot and store them into another array, and then loop through the items from that array and add the slot widgets from that, like this:

 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/3X/0/b/0b1a2d346d395cced9151e65302ff284285eee84.jpeg)

but there are two problems, first the save dates would be stored into that array on the same order as the slot numbers, nothing would change, and second the slots date array don’t have any reference to the slot ID number so I wouldn’t be able to add the widgets based on saved date.

so

1. how can I sort an array with random dates by their date?  
2 how can I associate that array of dates to their slot number?

---

<div class="post-metadata">

**Author:** ![fael097](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/fael097/32/250778_2.png) [@fael097](https://forums.unrealengine.com/u/fael097)\
**Post date:** [January 22, 2022, 3:08am UTC](https://forums.unrealengine.com/t/how-to-sort-by-newest-through-random-items-from-an-array/273640/5 "2022-01-22T03:08:02Z")

</div>

Ok bear with me, I used a map (dictionary) to associate datetime values to their slot numbers.

 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/3X/d/b/db5befa559eb09c640796d62b9099811bf960270.jpeg)

then when the first loop is done, it loops through the array of saved dates and finds their respective slot in the map, finally adding them to their parent scroll box.

Now I just need a way to sort that Slots Date array by date. any ideas?

---

<div class="post-metadata">

**Author:** ![fael097](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/fael097/32/250778_2.png) [@fael097](https://forums.unrealengine.com/u/fael097)\
**Post date:** [January 22, 2022, 6:08am UTC](https://forums.unrealengine.com/t/how-to-sort-by-newest-through-random-items-from-an-array/273640/6 "2022-01-22T06:08:15Z")

</div>

(if interested, for the complete answer check the entire thread util this post)

Ok so I found this

[https://answers.unrealengine.com/questions/953370/is-there-any-way-to-retrieve-the-most-recent-datet.html?sort=oldest](https://answers.unrealengine.com/questions/953370/is-there-any-way-to-retrieve-the-most-recent-datet.html?sort=oldest)

(answered by ClockworkOcean himself) and installed the plugin to my engine. A simple custom “sort date time array” node that ships with the plugin did the trick, it even has a reverse option so you can set from older to newer or the other way around. pretty neat node if you ask me.

![image](https://d3kjluh73b9h9o.cloudfront.net/original/3X/f/c/fcd09a171813651b350683351e26e7c37890e0f1.png)

I just had to add a few clear nodes to clear the date array and the map dictionary before the initial loop so it wouldn’t add duplicate entries, and now it works like a charm. neat!

only question remaining, is there a way to replicate this node’s functionality in blueprints? I’m not a big fan on relying on external 3rd party dev plugins, cuz they can get discontinued and then I’d be stuck to a certain engine version. will sure use this in the foreseeable future but it would be great to have this as a blueprint function.

Well, in fact I guess I should mark this as solved since it’s working, and ask about sorting a datetime array in another thread?

---

<div class="post-metadata">

**Author:** ![midgunner66](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/midgunner66/32/466711_2.png) [@midgunner66](https://forums.unrealengine.com/u/midgunner66)\
**Post date:** [January 23, 2022, 3:52am UTC](https://forums.unrealengine.com/t/how-to-sort-by-newest-through-random-items-from-an-array/273640/7 "2022-01-23T03:52:43Z")

</div>

Reposting my solution from another thread:

> [@How to sort a random DateTime array from oldest to newest?](https://forums.unrealengine.com/t/how-to-sort-a-random-datetime-array-from-oldest-to-newest/274128/9):
>
> you don’t need to sort anything because all saves are already going to be played in chronological order, so you’ll always know which is the most recent. Basically, the most recent save is just the one that was played last, and the one that was played last is the one that is being played currently (i.e. the one that the player picks).
> 
> So all you have to do is put the save that is being playing currently at the top of the array (index 0), and it’ll already be sorted.
> 
> > **Example (Newest to Oldest)**
> >
> > NewGame → Save0  
> > SaveArray = {Save0}
> > 
> > NewGame → Save1  
> > SaveArray = {Save1, Save0}
> > 
> > NewGame → Save2  
> > SaveArray = {Save2, Save1, Save0}
> > 
> > LoadGame → Save0  
> > SaveArray = {Save0, Save2, Save1}
> > 
> > NewGame-\> Save3  
> > Save Array = {Save3, Save0, Save2, Save1}
> > 
> > LoadGame → Save2  
> > SaveArray = {Save2, Save3, Save0, Save1}
> 
> And to have it oldest to newest, you would either (A) add them at the **end** of the array, or (B) read the array in reverse.

And a running example with nodes in this post:

> [@How to sort a random DateTime array from oldest to newest?](https://forums.unrealengine.com/t/how-to-sort-a-random-datetime-array-from-oldest-to-newest/274128/13):
>
> When adding a new save, you just add it to the beginning of the array (index 0): [image] And when loading a save, you move the save to the beginning of the array: Here’s what that looks like (displaying time only); notice how it always stays sorted: Also, you can see that the date isn’t even used, so this works without storing the date. All we are doing is moving the save that the player chooses to the top; the sorting happens naturally.

---

<div class="post-metadata">

**Author:** ![fael097](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/fael097/32/250778_2.png) [@fael097](https://forums.unrealengine.com/u/fael097)\
**Post date:** [January 23, 2022, 11:28pm UTC](https://forums.unrealengine.com/t/how-to-sort-by-newest-through-random-items-from-an-array/273640/8 "2022-01-23T23:28:13Z")

</div>

this is way better than all the gibberish I posted above, kudos
