C#List排序

//排序
    private static void ListSort(List<MD.Entity.User> infoList, string field, string rule)
    {
        if (!string.IsNullOrEmpty(field) && !string.IsNullOrEmpty(rule))
        {
            try
            {
                infoList.Sort(delegate(MD.Entity.User model1, MD.Entity.User model2)
                {
                    Type t = typeof(MD.Entity.User);
                    PropertyInfo pro = t.GetProperty(field);
                    return rule.ToLower().Equals("asc") ? pro.GetValue(model1, null).ToString().CompareTo(pro.GetValue(model2, null).ToString()) : pro.GetValue(model2, null).ToString().CompareTo(pro.GetValue(model1, null).ToString());
                });
            }
            catch (Exception ex) { }
        }
    }

 

posted @ 2013-06-21 17:04  lonely_rain  阅读(281)  评论(0编辑  收藏  举报