这样就可以了:
public class CustomComparer:System.Collections.IComparer
{
public int Compare(object x, object y){
string s1 = (string)x;
string s2 = (string)y;
if (s1.Length > s2.Length) return 1;
if (s1.Length < s2.Length) return -1;
for (int i = 0; i < s1.Length; i++) {
if (s1[i] > s2[i]) return 1;
if (s1[i] < s2[i]) return -1;
}
return 0;
}
}
应用:
string[] str = new string[]{"A1","A2","A10"};
Array.Sort(str, new CustomComparer());
for (int i = 0; i < str.Length; i++)
Console.WriteLine(str[i]);