You can use the windows-builtin natural-sort(like f.e. files are ordered) StrCmpLogicalW
:
Compares two Unicode strings. Digits in the strings are considered as numerical content rather than
text. This test is not case-sensitive.
[SuppressUnmanagedCodeSecurity]
internal static class SafeNativeMethods
{
[DllImport("shlwapi.dll", CharSet = CharSet.Unicode)]
public static extern int StrCmpLogicalW(string psz1, string psz2);
}
public sealed class NaturalStringComparer : IComparer<string>
{
public int Compare(string a, string b)
{
return SafeNativeMethods.StrCmpLogicalW(a, b);
}
}
Then the code to sort the DataTable
is easy with LINQ:
table = table.AsEnumerable()
.OrderBy(r => r.Field<string>("CourseNr"), new NaturalStringComparer())
.CopyToDataTable();
(credits for NaturalStringComparer
to: https://stackoverflow.com/a/248613/284240)