根据笔划进行汉字排序
class Program
{
static void Main(string[] args)
{
DataTable dt = new DataTable();
dt.Columns.Add("Name", typeof(string));
string str = "加快转变发展方式促进广州外贸增长";
for (int i = 0; i < str.Length; i++)
{
dt.Rows.Add(new object[] { str[i] });
}
DataRow[] rows = dt.Select("1=1");
Console.WriteLine("===原记录=======");
foreach (DataRow row in rows)
{
Console.Write(row["Name"]);
}
Console.WriteLine();
Array.Sort<DataRow>(rows, new HanziComparer());
Console.WriteLine("===排序后=======");
foreach (DataRow row in rows)
{
Console.Write(row["Name"]);
}
Console.ReadLine();
}
}
public class HanziComparer : IComparer<DataRow>
{
#region IComparer<DataRow> 成员
public int Compare(DataRow x, DataRow y)
{
//设置汉字比较方式
System.Globalization.CultureInfo culture = new System.Globalization.CultureInfo(0x00020804);
//按照笔画顺序比较两个字符串
return string.Compare(x["name"].ToString(), y["name"].ToString(), true, culture);
}
#endregion
}
{
static void Main(string[] args)
{
DataTable dt = new DataTable();
dt.Columns.Add("Name", typeof(string));
string str = "加快转变发展方式促进广州外贸增长";
for (int i = 0; i < str.Length; i++)
{
dt.Rows.Add(new object[] { str[i] });
}
DataRow[] rows = dt.Select("1=1");
Console.WriteLine("===原记录=======");
foreach (DataRow row in rows)
{
Console.Write(row["Name"]);
}
Console.WriteLine();
Array.Sort<DataRow>(rows, new HanziComparer());
Console.WriteLine("===排序后=======");
foreach (DataRow row in rows)
{
Console.Write(row["Name"]);
}
Console.ReadLine();
}
}
public class HanziComparer : IComparer<DataRow>
{
#region IComparer<DataRow> 成员
public int Compare(DataRow x, DataRow y)
{
//设置汉字比较方式
System.Globalization.CultureInfo culture = new System.Globalization.CultureInfo(0x00020804);
//按照笔画顺序比较两个字符串
return string.Compare(x["name"].ToString(), y["name"].ToString(), true, culture);
}
#endregion
}
系统输入为:
===原记录=======
加快转变发展方式促进广州外贸增长
===排序后=======
广方长加发外州式快进转促变贸展增