日期:2014-05-17 浏览次数:20937 次
DataTable dt = new DataTable();
dt.Columns.Add("name");
dt.Rows.Add("varchar2");
dt.Rows.Add("VARCHAR2");
dt.Rows.Add("zarchar2");
dt.Rows.Add("Varchar3");
dt.Rows.Add("varchar3");
List<DataRow> rows = dt.Rows.Cast<DataRow>().OrderBy(x => x["name"].ToString(), new A<string>()).ToList();
DataTable resultDt = dt.Clone();
rows.ForEach(x => resultDt.ImportRow(x));
GridView1.DataSource = resultDt;
GridView1.DataBind();
public class A<T> : IComparer<T>
{
public int Compare(T x, T y)
{
string s1 = x.ToString();
string s2 = y.ToString();
int minLength = Math.Min(s1.Length, s2.Length);
for (int i = 0; i < minLength; i++)
{
int result = s1[i].CompareTo(s2[i]);
if (result == 0)
continue;
else
return result;
}
if (s1.Length == s2.Length)
return 0;
else
return s1.Length > s2.Length ? 1 : -1;
}
}