You are looking for a "natural sort order".
It turns out that the Windows API provides a StrCmpLogicalW() function that we can call using P/Invoke to solve the issue.
Assuming that you are trying to sort a List<>
or an array, you can wrap it in an extension method to make it easier to call:
public static class ListAndArrayExt
{
[DllImport("shlwapi.dll", CharSet = CharSet.Unicode)]
private static extern int StrCmpLogicalW(string lhs, string rhs);
public static void SortNatural(this List<string> self)
{
self.Sort(StrCmpLogicalW);
}
public static void SortNatural(this string[] self)
{
Array.Sort(self, StrCmpLogicalW);
}
}
You can then use it to sort a List<>
in natural sort order. Here's a complete compilable Console app to demonstrate:
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
namespace ConsoleApp1
{
public static class ListAndArrayExt
{
[DllImport("shlwapi.dll", CharSet = CharSet.Unicode)]
private static extern int StrCmpLogicalW(string lhs, string rhs);
public static void SortNatural(this List<string> self)
{
self.Sort(StrCmpLogicalW);
}
public static void SortNatural(this string[] self)
{
Array.Sort(self, StrCmpLogicalW);
}
}
class Program
{
void run()
{
var strings = new List<string>
{
"1-A",
"1-B",
"10-A",
"10-B",
"9-A",
"9-B"
};
strings.SortNatural();
foreach (var s in strings)
Console.WriteLine(s);
}
static void Main()
{
new Program().run();
}
}
}
This prints out the strings like so:
1-A
1-B
9-A
9-B
10-A
10-B