C# Sorting numbered file names in List<string>

Viewed 47

I have some file names in a List but they are not in order. (I'm looping through a Directory and adding them to the list)

Example Current Output :

    'file 0.webp',
    'file 1.webp',
    'file 10.webp',
    'file 11.webp',
    'file 12.webp',
    'file 13.webp',
    'file 14.webp',
    'file 15.webp',
    'file 16.webp',
    'file 17.webp',
    'file 18.webp',
    'file 19.webp',
    'file 2.webp',
    'file 20.webp',
    'file 3.webp',
    'file 4.webp',
    'file 5.webp',
    'file 6.webp',
    'file 7.webp',
    'file 8.webp',
    'file 9.webp'

Example Expected Output :

    'file 0.webp',
    'file 1.webp',
    'file 2.webp',
    'file 3.webp',
    'file 4.webp',
    'file 5.webp',
    'file 6.webp',
    'file 7.webp',
    'file 8.webp',
    'file 9.webp',
    'file 10.webp',
    'file 11.webp',
    'file 12.webp',
    'file 13.webp',
    'file 14.webp',
    'file 15.webp',
    'file 16.webp',
    'file 17.webp',
    'file 18.webp',
    'file 19.webp',
    'file 20.webp'

Is there any way to make them be in order? Thanks!

This Is The Code I'm using.

   public Dictionary<string, List<string>> GetAnimationNames()
    {
        string FilePath = "D:\\Games\\GameFiles\\TargetFolder\\images\\Converted";
        Dictionary<string, List<string>> AnimationNames = new Dictionary<string, List<string>>();
        foreach (string fileName in Directory.GetFiles(FilePath))
        {
            string Key = "";

            if (fileName.EndsWith(".jpg"))
            {
                string[] AnimationName = Path.GetFileName(fileName).Replace(".jpg", "").Split(' ');
                if (AnimationName.Length <= 1) continue;
                Key = AnimationName[0];
                if (!AnimationNames.ContainsKey(AnimationName[0]))
                {
                    AnimationNames.Add(Key, new List<string>());
                }
                AnimationNames[Key].Add(fileName);
            }
        }
        
        return AnimationNames;
    }

I want AnimationNames[Key] List to be Sorted Correctly

Thanks!

0 Answers
Related