You sort the lines (in alphabetical order), so yes, "10" comes to "7".
My solution converts "10,7b, 8,7a, 9b" to "7a, 7b, 8,9b, 10" (first sort by integer prefix, then by the substring itself).
Helper method for parsing a string prefix:
private static int IntPrefix(string s) => s .TakeWhile(ch => ch >= '0' && ch <= '9') .Aggregate(0, (a, c) => 10 * a + (c - '0'));
Sort substrings by integer prefix, and then by the string itself:
classes.Split(',') // string[] .Select(s => new { s, i = IntPrefix(s) }) // IEnumerable<{ s: string, i: int }> .OrderBy(si => si.i) // IEnumerable<{ s: string, i: int }> .ThenBy(si => si.s) // IEnumerable<{ s: string, i: int }> .Select(si => si.s) // IEnumerable<string>
One liner (with string.Join ):
var result = string.Join(",", classes.Split(',').Select(s => new {s, i = IntPrefix(s)}).OrderBy(si => si.i).ThenBy(si => si.s).Select(si => si.s));
source share