C#按汉字拼音首字母排序

来源:互联网 发布:淘宝美工可以自学吗 编辑:程序博客网 时间:2024/04/30 04:21

可参考以下博客。

C# 使用微软的Visual Studio International Pack 类库提取汉字拼音首字母 - 影子科技 - 博客园
http://www.cnblogs.com/yazdao/archive/2011/06/04/2072488.html

C#汉字转换拼音技术详解(高性能) - 历史的驱动 - 博客园
http://www.cnblogs.com/lilin123/archive/2012/11/01/2749169.html

按汉字的拼音排序(c#实现) - 焦涛 - 博客园
http://www.cnblogs.com/Joetao/articles/2022500.html

汉字转换为汉语拼音(全拼/首字母简拼) - 任家 - 博客园
http://www.cnblogs.com/Gnepner/archive/2011/08/08/2130727.html


一、首先将汉字转为拼音首字母缩写

使用微软提供的方法获取到的是全拼再截取,故效率较低,以下为最终使用的代码。

      public staticstring GetFirstPY(stringstr)

       {

           string ret = string.Empty;

           foreach (char cin str)

           {

               ret += GetPYChar(c);

           }

           return ret;

       }

 

       private static string GetPYChar(charc)

       {

           string str = c.ToString();

           if ((int)c>= 32 && (int)c <= 126)

           {

               return str;

           }

 

           byte[] array = newbyte[2];

           array = System.Text.Encoding.Default.GetBytes(str);

           int i = (short)(array[0]-'\0') * 256 + ((short)(array[1]-'\0'));

           if (i < 0xB0A1) return"*";

           if (i < 0xB0C5) return"A";

           if (i < 0xB2C1) return"B";

           if (i < 0xB4EE) return"C";

           if (i < 0xB6EA) return"D";

           if (i < 0xB7A2) return"E";

           if (i < 0xB8C1) return"F";

           if (i < 0xB9FE) return"G";

           if (i < 0xBBF7) return"H";

           if (i < 0xBFA6) return"J";

           if (i < 0xC0AC) return"K";

           if (i < 0xC2E8) return"L";

           if (i < 0xC4C3) return"M";

           if(i < 0xC5B6) return "N";

           if (i < 0xC5BE) return"O";

           if (i < 0xC6DA) return"P";

           if (i < 0xC8BB) return"Q";

           if (i < 0xC8F6) return"R";

           if (i < 0xCBFA) return"S";

           if (i < 0xCDDA) return"T";

           if (i < 0xCEF4) return"W";

           if (i < 0xD1B9) return"X";

           if (i < 0xD4D1) return"Y";

           if (i < 0xD7FA) return"Z";

 

           return "*";

       }

二、构建一个List,保存汉字和其对应拼音缩写,然后排序。

str = str.OrderBy(x => x.pinyin).ToList();

即可获得排序后的列表。


0 0