A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 徐艳勇 中级黑马   /  2012-10-13 13:45  /  1390 人查看  /  1 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

  1. public static IList<T> Sort<T>(this IList<T> source, string sortProper, bool asc)
  2. {
  3. if (source != null && source.Any())
  4. {
  5. var properties = typeof(T).GetProperties();
  6. PropertyInfo pro = null;
  7. foreach (var item in properties)
  8. {
  9. if (item.Name.ToUpper().Equals(sortProper.ToUpper()))
  10. {
  11. pro = item;
  12. break;
  13. }
  14. }
  15. for (int i = 0; i < source.Count; i++)
  16. {
  17. T t;
  18. for (int k = 0; k < source.Count; k++)
  19. {
  20. int compare = pro.GetValue(source[i], null).ToString().CompareTo(pro.GetValue(source[k], null).ToString());
  21. if ((asc && compare <0)||(!asc && compare > 0))
  22. {
  23. t = source[i];
  24. source[i] = source[k];
  25. source[k] = t;
  26. }
  27. }
  28. }
  29. return source;
  30. }
  31. return null;
  32. }
复制代码

评分

参与人数 1技术分 +1 收起 理由
宋天琪 + 1

查看全部评分

1 个回复

倒序浏览
值得学习ing!
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马