利用自定义特性实现List的多属性排序
2015-03-15 21:33
127 查看
知道linq有order by的功能,但是还是动手研究了一下,算是多实践实践反射。这篇算是笔记,直接上代码:
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
namespace TestMultiplePropertySort
{
class Program
{
static void Main(string[] args)
{
#region 简单测试数据
var list = new List<MyClass>()
{
new MyClass()
{
P1="h3",
P2=1,
P3=DateTime.Now
},
new MyClass()
{
P1="h2",
P2=3,
P3=DateTime.Now.AddHours(-1)
},
new MyClass()
{
P1="h1",
P2=2,
P3=DateTime.Now.AddHours(1)
},
new MyClass()
{
P1="h3",
P2=1,
P3=DateTime.Now
},
new MyClass()
{
P1="h1",
P2=1,
P3=DateTime.Now
},
new MyClass()
{
P1="h2",
P2=2,
P3=DateTime.Now.AddHours(1)
},
};
#endregion
//调用多字段排序
SortMutiplePropertyHelper<MyClass>.SortMutipleProperty(list);
//可以复用
SortMutiplePropertyHelper<MySecondClass>.SortMutipleProperty(new List<MySecondClass>());
//输出排序结果
list.ForEach(m => Trace.WriteLine(m.ToString()));
}
}
public class MyClass
{
[SortOrder(0)]
public string P1 { get; set; }
[SortOrder(1)]
public int P2 { get; set; }
[SortOrder(2)]
public DateTime P3 { get; set; }
public override string ToString()
{
return P1.ToString() + "," + P2.ToString() + "," + P3.ToString();
}
}
public class MySecondClass
{
}
[AttributeUsage(AttributeTargets.Property, AllowMultiple = false, Inherited = false)]
public class SortOrderAttribute : Attribute
{
public int Order { get; set; }
public SortOrderAttribute(int order)
{
this.Order = order;
}
}
public class SortMutiplePropertyHelper<T> where T : class ,new()
{
/// <summary>
/// 保存属性和顺序的字典
/// </summary>
public static readonly Dictionary<PropertyInfo, SortOrderAttribute> attrDic = new Dictionary<PropertyInfo, SortOrderAttribute>();
static SortMutiplePropertyHelper()
{
//初始化order字段
Type t = typeof(T);
foreach (var prop in t.GetProperties())
{
foreach (var sortOrderAttribute in prop.GetCustomAttributes(typeof(SortOrderAttribute), false))
{
if (sortOrderAttribute is SortOrderAttribute)
{
attrDic.Add(prop, sortOrderAttribute as SortOrderAttribute);
break;
}
}
}
}
public static void SortMutipleProperty(List<T> list)
{
list.Sort((t1, t2) =>
{
int result = 0;
foreach (var attr in attrDic.OrderBy(key => key.Value.Order))
{
//这里简单的把属性转成字符串对比,比较靠谱的做法应当是针对不同的类型去做不同的比较。
string v1 = attr.Key.GetValue(t1).ToString();
string v2 = attr.Key.GetValue(t2).ToString();
result = v1.CompareTo(v2);
if (result != 0)
{
break;
}
}
return result;
});
}
}
}
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
namespace TestMultiplePropertySort
{
class Program
{
static void Main(string[] args)
{
#region 简单测试数据
var list = new List<MyClass>()
{
new MyClass()
{
P1="h3",
P2=1,
P3=DateTime.Now
},
new MyClass()
{
P1="h2",
P2=3,
P3=DateTime.Now.AddHours(-1)
},
new MyClass()
{
P1="h1",
P2=2,
P3=DateTime.Now.AddHours(1)
},
new MyClass()
{
P1="h3",
P2=1,
P3=DateTime.Now
},
new MyClass()
{
P1="h1",
P2=1,
P3=DateTime.Now
},
new MyClass()
{
P1="h2",
P2=2,
P3=DateTime.Now.AddHours(1)
},
};
#endregion
//调用多字段排序
SortMutiplePropertyHelper<MyClass>.SortMutipleProperty(list);
//可以复用
SortMutiplePropertyHelper<MySecondClass>.SortMutipleProperty(new List<MySecondClass>());
//输出排序结果
list.ForEach(m => Trace.WriteLine(m.ToString()));
}
}
public class MyClass
{
[SortOrder(0)]
public string P1 { get; set; }
[SortOrder(1)]
public int P2 { get; set; }
[SortOrder(2)]
public DateTime P3 { get; set; }
public override string ToString()
{
return P1.ToString() + "," + P2.ToString() + "," + P3.ToString();
}
}
public class MySecondClass
{
}
[AttributeUsage(AttributeTargets.Property, AllowMultiple = false, Inherited = false)]
public class SortOrderAttribute : Attribute
{
public int Order { get; set; }
public SortOrderAttribute(int order)
{
this.Order = order;
}
}
public class SortMutiplePropertyHelper<T> where T : class ,new()
{
/// <summary>
/// 保存属性和顺序的字典
/// </summary>
public static readonly Dictionary<PropertyInfo, SortOrderAttribute> attrDic = new Dictionary<PropertyInfo, SortOrderAttribute>();
static SortMutiplePropertyHelper()
{
//初始化order字段
Type t = typeof(T);
foreach (var prop in t.GetProperties())
{
foreach (var sortOrderAttribute in prop.GetCustomAttributes(typeof(SortOrderAttribute), false))
{
if (sortOrderAttribute is SortOrderAttribute)
{
attrDic.Add(prop, sortOrderAttribute as SortOrderAttribute);
break;
}
}
}
}
public static void SortMutipleProperty(List<T> list)
{
list.Sort((t1, t2) =>
{
int result = 0;
foreach (var attr in attrDic.OrderBy(key => key.Value.Order))
{
//这里简单的把属性转成字符串对比,比较靠谱的做法应当是针对不同的类型去做不同的比较。
string v1 = attr.Key.GetValue(t1).ToString();
string v2 = attr.Key.GetValue(t2).ToString();
result = v1.CompareTo(v2);
if (result != 0)
{
break;
}
}
return result;
});
}
}
}
相关文章推荐
- 利用反射和自定义特性实现基本数据存取操作自动获取
- 利用比较接口实现List中的对象排序
- 实现List<>自定义排序
- Java:集合,对列表(List)中的自定义对象按属性(字段)排序(正序、倒序)的方法
- 利用反射 和 comparator 实现List 的简单排序
- [转]winForm:利用窗体的自定义属性实现窗体传值
- C#中List实现按照对象属性排序
- List自定义排序 让Collections.sort 排序支持对象属性
- Java实现根据List中对象的相应属性进行排序
- java Collections.sort()实现List排序的默认方法和自定义方法
- 利用CSS的Expression自定义属性来实现表格的OnMouseOver 特效
- 日文,中文,韩文,利用Arrays.sort()对非英文排序,并与原值比较,返回(-1,0,1), 实现 Comparator接口, list 排序
- 利用比较接口实现List中的对象排序
- List<T>泛型数组API自带的Sort() 排序方法根据自定义排序实现
- 利用sql2005的新特性实现根据子表条件得到的主表键且按其排序取出对应主表记录的方法
- 利用case或者decode实现自定义顺序排序
- 利用反射+自定义特性实现简单ORM(二),“无SQL”的数据库增删改查
- Flex Gumbo中如何利用itemRenderer属性自定义FxList内项目的例子
- 利用prototype属性自定义对象,实现对象继承等面向对象设计
- List<>根据指定属性排序(实现IComparer接口)