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());