I have a List<string>
that I need to sort by alphanumeric but it also has a decimal. The sample looks like this:
E11.9
E13.9
E10.9
E11.65
E10.65
E11.69
E13.10
E10.10
The output I need should look like this:
E10.10
E10.65
E10.9
E11.69
E11.9
etc..
I've tried this code:
result.Sort((s1, s2) =>
{
string pattern = "([A-Za-z])([0-9]+)";
string h1 = Regex.Match(s1, pattern).Groups[1].Value;
string h2 = Regex.Match(s2, pattern).Groups[1].Value;
if (h1 != h2)
return h1.CompareTo(h2);
string t1 = Regex.Match(s1, pattern).Groups[2].Value;
string t2 = Regex.Match(s2, pattern).Groups[2].Value;
return int.Parse(t1).CompareTo(int.Parse(t2));
});
But it only seems to sort by the Letter first, then by the digits prior to the decimal place. So this is what I get:
E10.9
E10.65
E10.10
E11.9
E11.69
etc..
Am I missing something in the regex? Or is there a better way to accomplish this?