C#字符串数组排序
//排序只带字符的数组,不带数字的 private string[] aa ={ "a ", "c ", "b "}; Array.Sort(aa); MessageBox.Show(aa[0]); MessageBox.Show(aa[1]); MessageBox.Show(aa[2]); 结果是:a,b,c
如果想实现
Array.Sort(arr);
不对!!!!!!!!!!
比如: A1,A2,A10
用Array.Sort(arr);排出来就是
A1,A10,A2
而我要的是
A1,A2,A10
这样就可以了: 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]);
但是这样不对带有字符的字符排序。