Create a filtered list of World Names

I want to create a dropdown with the world names the user selects, and I want it to work kinda like how Minecraft does, where the user creates a world, and adds it to the top of the World Selection “screen”. Kinda like this:

//A, B, C, D    <----Default array
//D, C, B, A    <----New, formatted array
//LOAD A
//A, D, C, B    <----New, formatted array when 'A' loaded
//LOAD C
//C, A, D, B    <----New, formatted array when 'C' loaded
//CREATE E
//E, C, A, D, B <----New, formatted array when 'E' created

I have the list(Array that is converted into a list) saved into a .json file, that when a new world is created, is saved to the end of that array. I also have a lastPlayedWorld integer that stores the index of the last played world in the array.
Sorry if that’s a lot of information, no idea if anything actually made sense lol. Just ask if you need any clarification.
Thank you in advance!

Well basically you have two solutions:

  • When creating a new world, add it at the top of the list by using WorldsList.InsertAt(0, newWorld);
    or
  • After loading the worlds from Json, order them in descending order by creation date (if you have this info) or id (if incremental): WorldList = loadedWorldsFromJson.OrderByDescending(x => x.CreationDate).ToList(); (don’t forget to add using System.Linq;)
1 Like

Adding a creation date doesn’t sound like a bad idea…I’m also thinking about having an openedDate that’ll change when the user opens/loads in the world, and sort by that. Thank you SOO much for your suggestion!

1 Like

A standard unsorted list should be all you need for this.

When a new world is added insert it at the beginning of the list as suggested by apkimmerling and when a world is loaded, remove it from the list and then re-insert it at the beginning of the list.

https://learn.microsoft.com/en-us/dotnet/api/system.datetime.now?view=net-6.0

You will find a lot of information and even examples on basic things like that super quickly!
Simply searching exactly what you asked above: “date and time in c#”, should yield the above result :slight_smile:

1 Like