原文链接:http://blog.csdn.net/wanzhuan2010/article/details/6205884

[c-sharp:nogutter:firstline[0]] view plaincopyprint?
  1. using System; 
  2. using System.Collections.Generic; 
  3. using System.Linq; 
  4. using System.Text; 
  5. namespace ListSort 
  6.     class Program 
  7.     { 
  8.         static void Main(string[] args) 
  9.         { 
  10.             List<Customer> listCustomer = new List<Customer>(); 
  11.             listCustomer.Add(new Customer { name = "客户1", id = 0 }); 
  12.             listCustomer.Add(new Customer { name = "客户2", id = 1 }); 
  13.             listCustomer.Add(new Customer { name = "客户3", id = 5 }); 
  14.             listCustomer.Add(new Customer { name = "客户4", id = 3 }); 
  15.             listCustomer.Add(new Customer { name = "客户5", id = 4 }); 
  16.             listCustomer.Add(new Customer { name = "客户6", id = 5 }); 
  17.             ///升序 
  18.             List<Customer> listCustomer1 = listCustomer.OrderBy(s => s.id).ToList<Customer>(); 
  19.             //降序 
  20.             List<Customer> listCustomer2 = listCustomer.OrderByDescending(s => s.id).ToList<Customer>(); 
  21.             //Linq排序方式 
  22.             List<Customer> listCustomer3 = (from c in listCustomer 
  23.                                             orderby c.id descending //ascending 
  24.                                             select c).ToList<Customer>(); 
  25.             Console.WriteLine("List.OrderBy方法升序排序"); 
  26.             foreach (Customer customer in listCustomer1) 
  27.             { 
  28.                 Console.WriteLine(customer.name); 
  29.             } 
  30.             Console.WriteLine("List.OrderByDescending方法降序排序"); 
  31.             foreach (Customer customer in listCustomer2) 
  32.             { 
  33.                 Console.WriteLine(customer.name); 
  34.             } 
  35.             Console.WriteLine("Linq方法降序排序"); 
  36.             foreach (Customer customer in listCustomer3) 
  37.             { 
  38.                 Console.WriteLine(customer.name); 
  39.             } 
  40.             Console.ReadKey(); 
  41.         } 
  42.     } 
  43.     class Customer 
  44.     { 
  45.         public int id { get; set; } 
  46.         public string name { get; set; } 
  47.     } 

 

效果展示: