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) { }

        }

    }

 

你可能感兴趣的:(list排序)