void Main() { var list = new List<string>() { "AB1235", "AB9999", "AB99", "AB999", "AB12", "AB123", "14", "12", "123", }; var ordered = list.OrderBy(l => PrependDigits(l)); foreach(var item in ordered) Console.WriteLine(item); } public static Regex regexPrependDigits = new Regex(@"\d+", RegexOptions.Compiled); public static string PrependDigits(string s) { if (string.IsNullOrWhiteSpace(s)) return s; return regexPrependDigits.Replace(s, m => m.Value.PadLeft(10, '0')); }
Output:
12 14 123 AB12 AB99 AB123 AB999 AB1235 AB9999
Typescript prependdigits version
function prependDigits(s: string, len: number = 10): string { return String(s).replace(/\d+/, (m) => m.padStart(len, "0")) }
335200cookie-checkC# simple natural sort (with max digits)