-1

The problem is in this lines:

string savedGamesFolder = string.Format("{0}/Saved Games",
Application.persistentDataPath);

if (!Directory.Exists(savedGamesFolder))
{
     Directory.CreateDirectory(savedGamesFolder);
}
string[] directories = Directory.GetDirectories(savedGamesFolder);
Array.Sort(directories, new NaturalSortComparer());

in the array string[] directories in the order of 0,1,10,11,12,13...to 19 then 2 then 20,21,22,23 to 26 then 3,4,5,6,7,8,9

example of a directory name: C:/Users/test/AppData/LocalLow/DefaultCompany/my game/Saved Games\SaveSlot0 SavedGameSlot_1920x1080_2023-06-15_14-04-08

the next one contains SaveSlot1 then instead SaveSlot0 the third directory is with SaveSlot10 but on the hard disk the order is right: 0,1,2,3,4,5,6,7,8,9,10,11,12 to 27

I tried to sort the directories array with this line and the class NaturalSortComparer:

Array.Sort(directories, new NaturalSortComparer());

this is the class:

public class NaturalSortComparer : IComparer<string>
{
    public int Compare(string a, string b)
    {
        return StringComparer.Create(System.Globalization.CultureInfo.CurrentCulture, true).Compare(a, b);
    }
}

but still it's not the same order as on the hard disk.

I tried to add and using the class NaturalSortComparer but it didn't change much , still the order of directories in the array string[] directories is not the same as on the hard disk.

UPDATE :

I found that this class is working:

public class MyComparer : IComparer<string>
{
    [DllImport("Shlwapi.dll", SetLastError = true, CharSet = CharSet.Unicode)]
    private static extern int StrCmpLogicalW(string psz1, string psz2);

    public int Compare(string psz1, string psz2)
    {
        return StrCmpLogicalW(psz1, psz2);
    }
}

using:

string[] directories = Directory.GetDirectories(savedGamesFolder);
Array.Sort(directories, new MyComparer());
Daniel Lip
  • 27
  • 5
  • Almost certainly what you see of the directories on disk is a ***view*** provided by some tool such as file explorer. You have no way of knowing 'the order of how they were written' to disk other than to view to created date. – Ňɏssa Pøngjǣrdenlarp Jun 17 '23 at 00:27
  • @ŇɏssaPøngjǣrdenlarp I found a solution, updated my question with it. can you tell me why it's not good or maybe it's not a good solution? it is working. – Daniel Lip Jun 17 '23 at 03:14
  • If it sorts correctly, then you could post it as an answer. If you have doubts, then please specify. There are also natural sort solutions that don't use P/Invoke – Hans Kesting Jun 17 '23 at 05:50
  • https://stackoverflow.com/questions/248603/natural-sort-order-in-c-sharp – Hans Kesting Jun 17 '23 at 05:54
  • 1
    `Shlwapi.dll` solution will work on windows only – derHugo Jun 17 '23 at 09:03

1 Answers1

0

Instead of using the names you could rather sort by the actual creation time.

Simply go through DirectoryInfo instead and then e.g. sort by CreationTimeUtc using Linq OrderBy like e.g.

var savedGamesFolderPath = Path.Combine(Application.persistentDataPath, "Saved Games");
var savedGameFolder = new DirectoryInfo(savedGamesFolderPath);

if (!savedGameFolder.Exists)
{
    savedGamesFolder.Create();
}

// in general it is cheaper to use ToList with linq as it doesn't have to crop the length to an exact array size 
var directories = savedGamesFolder.GetDirectories().OrderBy(d => d.CreationTimeUtc).ToList();
derHugo
  • 83,094
  • 9
  • 75
  • 115