zoukankan      html  css  js  c++  java
  • list排序

    今天要对List排序,上网查了很多方法都感觉比较麻烦,现在终于找到了两种比较简便的方式,在此写出来,防止忘记!同时供大家参考!

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

     

    效果展示:

  • 相关阅读:
    [51nod1685]第k大区间
    [51nod1515]明辨是非
    centos中jdk配置 平凡的人生
    centos中apachetomcat的配置 平凡的人生
    centos6.5中gitlab的搭建 平凡的人生
    centos精简系统 源码安装客户端git 平凡的人生
    Vue学习笔记vue调试工具vuedevtools安装及使用
    Django Admin 实现三级联动的示例代码(省市区)===>小白级
    Vue学习笔记基于CDN引入方式简单前后端分离项目学习(Vue+Element+Axios)
    Django Admin 配置和定制基本功能(基本二次开发配置)
  • 原文地址:https://www.cnblogs.com/txx314/p/5269002.html
Copyright © 2011-2022 走看看