zoukankan      html  css  js  c++  java
  • 【转】关于List排序的时效性

    不多说了,就是说明List排序的时效性,仅仅用来备忘,改造自: http://blog.csdn.net/wanzhuan2010/article/details/6205884,感谢原作者

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

    结果展示:(新加入的成员没有参与排序)

  • 相关阅读:
    Python的集合和元组
    Python之 string 和 random方法
    Python文件读取常用方法
    Python文件读写之r+/w+/a+
    Python非空即真,非零即真
    【转】Python字符串格式化
    HTML5新增的form属性简介——张鑫旭
    CSS3选择器:nth-child和:nth-of-type之间的差异——张鑫旭
    CSS页面重构“鑫三无准则”之“无图片”准则——张鑫旭
    CSS垂直翻转/水平翻转提高web页面资源重用性——张鑫旭
  • 原文地址:https://www.cnblogs.com/snys98/p/4357561.html
Copyright © 2011-2022 走看看