第一种方式:
这种方式会受制于服务器的区域和语言设置。
1.首先添加一个排序类ChineseNameComparer
public class ChineseNameComparer : IComparer<string>
{public int Compare(string x, string y){if (x == null || y == null)return 0;var xFirstChar = x.Substring(0, 1);var yFirstChar = y.Substring(0, 1);return string.Compare(xFirstChar, yFirstChar, StringComparison.CurrentCulture);}
}
2.list排序
list = list.OrderBy(t => t.ChineseName, new ChineseNameComparer()).ToList();
第二种方式:引入Microsoft.International.Converters.PinYinConverter包
private string GetPinyin(string chineseName){string pinyin = string.Empty;foreach (char c in chineseName){if (ChineseChar.IsValidChar(c)){ChineseChar chineseChar = new ChineseChar(c);pinyin += chineseChar.Pinyins[0][0];}else{pinyin += c;}}return pinyin;}
获取到拼音后再list排序。