首页 > 代码库 > 使用OrderBy对List<Person>集合排序

使用OrderBy对List<Person>集合排序

        string sortOrder = Request.QueryString["sortOrder"];        

    string sortField = Request.QueryString["sortField"];

        List<Person> lstPer = new List<Person>();    

        List<Person> lstPerDesc = new List<Person>();//降序排序结果     

        List<Person> lstPerAsc = new List<Person>();//升序排序结果   

        for (int j = 0; j < 10; j++)        

        {          

       lstPer.Add(new Person() { ID = j, UserName = Guid.NewGuid().ToString() });        

     }        

     if (!string.IsNullOrEmpty(sortField) && !string.IsNullOrEmpty(sortOrder))        

        {      

                      if (sortOrder.ToUpper() == "DESC")           

                     {   

                                     lstPerDesc = lstPer.OrderByDescending(o => o.GetType().GetProperty("ID").GetValue(o, null)).ToList();          

                      } 

                      else  

                      {               

              lstPerAsc = lstPer.OrderBy(o => o.GetType().GetProperty("ID").GetValue(o, null)).ToList();          

                      }      

         }

 

 

 

 

   public class Person    

  {        

    public int ID { get; set; }        

    public string UserName { get; set; }

  }

 

使用OrderBy对List<Person>集合排序