private static readonly int INT_MAX_DIGITS = Int32.MaxValue.ToString().Length; public static string ConvertForNaturalOrdering(string path) { var sb = new StringBuilder(); var digitCache = new StringBuilder(); foreach (char ch in path) { if (Char.IsDigit(ch)) { digitCache.Append(ch); continue; } if (digitCache.Length > 0) { sb.Append(digitCache.ToString().PadLeft(INT_MAX_DIGITS, '0')); digitCache.Length = 0; } sb.Append(ch); } if (digitCache.Length > 0) sb.Append(digitCache.ToString().PadLeft(INT_MAX_DIGITS, '0')); return sb.ToString(); }
Here is how the method can be used.
var sorted = from path in new[] { "aab2", "aaa10", "aaa2", "aaa1 1" } orderby ConvertForNaturalOrdering(path) select path;
The 'sorted' enumeration will be in this order:
"aaa1 1", "aaa2", "aaa10", "aab2"
No comments:
Post a Comment