码迷,mamicode.com
首页 > 编程语言 > 详细

自定义排序方式

时间:2016-05-30 14:11:52      阅读:148      评论:0      收藏:0      [点我收藏+]

标签:

 

public class Custom_sort: IComparer<string>
{
List<string> _strs;
public Custom_sort(string[] strs)
{
_strs = strs.Distinct().ToList();
}
public int Compare(string x, string y)
{
if (x.Substring(x.Length - 2, 2) == y.Substring(y.Length - 2, 2))
{
return _strs.FindIndex(o => o.Equals(x.Substring(0, 2))) - _strs.FindIndex(o => o.Equals(y.Substring(0, 2)));
}
var defx = 0;
var defy = 0;
var xtrany= int.TryParse(x.Substring(x.Length - 2, 2), out defx);
var ytrany = int.TryParse(y.Substring(y.Length - 2, 2), out defy);
if (!xtrany && !ytrany)
{
return 0;
}
return defx - defy;
}
}

//需要写一个类  实现IComparer该接口的方法

 

 

DataTable dt = new DataTable();
dt.Columns.Add("Name");
dt.Rows.Add(new object[] { "SP15", });
dt.Rows.Add(new object[] { "SU16", });
dt.Rows.Add(new object[] { "SM16", });
dt.Rows.Add(new object[] { "AC1d", });
dt.Rows.Add(new object[] { "AC15", });
dt.Rows.Add(new object[] { "SM15", });
dt.Rows.Add(new object[] { "SP16", });
dt.Rows.Add(new object[] { "SU15", });
dt.Rows.Add(new object[] { "AC16", });
//dr.Fill(dt);

//调用一个Custom_sort类里的一个方法

//重写IComparer里的方法
var c = dt.AsEnumerable().OrderBy(o => o.Field<string>("Name"),coustOrder).CopyToDataTable();
var b = c;

自定义排序方式

标签:

原文地址:http://www.cnblogs.com/xiaojian1/p/5542150.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!