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之编写购物车(第二天)
    day6作业--游戏人生
    day7 socket网络编程基础
    day7 --socket网络编程基础
    Python类和人类
    day7 socket网络编程
    day7异常处理
    day7面向对象--反射
    day6面向对象--类的特殊成员方法
    使用BigDecimal来进行精确计算
  • 原文地址:https://www.cnblogs.com/snys98/p/4357561.html
Copyright © 2011-2022 走看看