工做中用到一個字符串排序,相似於windows中文件夾的排序,原本也沒有什麼,排序用冒泡算法就好了。算法
不過字符串對比,沒有找到系統自帶的方法,只好本身寫一個。windows
排序採用Asciid碼錶,例如1與a,則a是大的;spa
一樣字符的話,取長度最大的,例如aa與aaa,取aaa爲大;若是是aab與aaa,天然是aab爲最大;code
/// <summary> /// 比較兩個字符串的大值(按ascii) /// </summary> /// <param name="s1"></param> /// <param name="s2"></param> /// <returns></returns> private static string maxString(string s1, string s2) { //一些非空判斷 if (string.IsNullOrWhiteSpace(s1)) return s2; if (string.IsNullOrWhiteSpace(s2)) return s1; if (s1.Trim() == "") return s2; if (s2.Trim() == "") return s1; //對比 int minlen = s1.Length < s2.Length ? s1.Length : s2.Length; char[] c1 = s1.ToCharArray(); char[] c2 = s2.ToCharArray(); char[] max = null; for (int i = 0; i < minlen; i++) { if (c1[i] != c2[i]) { max = c1[i] > c2[i] ? c1 : c2; break; } } if (max == null) max = c1.Length > c2.Length ? c1 : c2; return new string(max); } /// <summary> /// 比較兩個字符串的小值(按ascii) /// </summary> /// <param name="s1"></param> /// <param name="s2"></param> /// <returns></returns> private static string minString(string s1, string s2) { //一些非空判斷 if (string.IsNullOrWhiteSpace(s1)) return s2; if (string.IsNullOrWhiteSpace(s2)) return s1; if (s1.Trim() == "") return s2; if (s2.Trim() == "") return s1; //對比 int minlen = s1.Length < s2.Length ? s1.Length : s2.Length; char[] c1 = s1.ToCharArray(); char[] c2 = s2.ToCharArray(); char[] min = null; for (int i = 0; i < minlen; i++) { if (c1[i] != c2[i]) { min = c1[i] < c2[i] ? c1 : c2; break; } } if (min == null) min = c1.Length < c2.Length ? c1 : c2; return new string(min); }