试试这个:
var text = "111,111A,222,411G,300,411Z,G411,AG500,A111,AZ600,ABQ,ZZZ,AAN";
var list = text.Split(',').ToList();
var result = list.OrderBy(i => i, new StringCompare());
foreach (var item in result)
{
Console.WriteLine(item);
}
字符串比较类:
class StringCompare : IComparer<string>
{
string[] exps = new[] { @"^\d+$", @"^\d+[a-zA-Z]+$", @"^[a-zA-Z]\d+$", @"^[a-zA-Z]+\d+$" };
public int Compare(string x, string y)
{
for (int i = 0; i < exps.Length; i++)
{
var isNumberx = Regex.IsMatch(x, exps[i]);
var isNumbery = Regex.IsMatch(y, exps[i]);
if (isNumberx && isNumbery)
return string.Compare(x, y);
else if (isNumberx)
return -1;
else if (isNumbery)
return 1;
//return string.Compare(x, y);
}
return string.Compare(x, y);
}
}
你会得到:
111
222
300
111A
411G
411Z
A111
G411
AG500
AZ600
AAN
ABQ
ZZZ