C# Linq 之 OrderBy

 

OrderBy、OrderByDescending、ThenBy和ThenByDescending。

由于 IOrderedEnumerable<TElement> 继承自 IEnumerable<T> ,因此可以对 OrderBy OrderByDescending 、或调用的结果调用
OrderBy OrderByDescending ThenBy ThenByDescending 。 这样做会引入一个新的主要顺序,该顺序将忽略以前建立的排序。

public static void Dome1()
        {
            List<Product> products = new List<Product> {
                                     new Product{
                                      Count=1,
                                       Name="产品12",
                                        CreateTime=new DateTime(2021,1,2),
                                         Price=100
                                     },
                                      new Product{
                                      Count=2,
                                       Name="产品2",
                                        CreateTime=new DateTime(2021,1,3),
                                         Price=500
                                     },
                                       new Product{
                                      Count=3,
                                       Name="产品3",
                                        CreateTime=new DateTime(2021,1,5),
                                         Price=600
                                     },
                                        new Product{
                                      Count=200,
                                       Name="产品4",
                                        CreateTime=new DateTime(2021,1,4),
                                         Price=100
                                     },
                                         new Product{
                                      Count=5,
                                       Name="产品5",
                                        CreateTime=new DateTime(2021,1,10),
                                         Price=300
                                     }
             };



            //根据时间正序,再按名称长度倒叙,再更具数量正序,最后根据价格倒叙


            var data = products.OrderBy(q => q.CreateTime);

            Console.WriteLine("===================================");
            foreach (var item in data)
            {
                Console.WriteLine($"{item.Name},生产日期:{item.CreateTime},数量:{item.Count},价格:{item.Price}");
            }
            Console.WriteLine("===================================");
            var data2 = data.OrderByDescending(ob => ob.Name.Length);
            foreach (var item in data2)
            {
                Console.WriteLine($"{item.Name},生产日期:{item.CreateTime},数量:{item.Count},价格:{item.Price}");
            }
            Console.WriteLine("===================================");
            var data3 = data2.ThenBy(a => a.Count);
            foreach (var item in data3)
            {
                Console.WriteLine($"{item.Name},生产日期:{item.CreateTime},数量:{item.Count},价格:{item.Price}");
            }
            Console.WriteLine("===================================");
            var data4 = data3.ThenByDescending(a => a.Price);
            foreach (var item in data4)
            {
                Console.WriteLine($"{item.Name},生产日期:{item.CreateTime},数量:{item.Count},价格:{item.Price}");
            }

            Console.WriteLine("===================================");

            var result = products.OrderBy(ob => ob.CreateTime)
                .ThenBy(ob => ob.Name.Length);
            foreach (var item in result)
            {
                Console.WriteLine($"{item.Name},生产日期:{item.CreateTime},数量:{item.Count},价格:{item.Price}");
            }

        }


        public static void Dome2()
        {
            List<Product> products = new List<Product> {
                                     new Product{
                                      Count=1,
                                       Name="产品12",
                                        CreateTime=new DateTime(2021,1,2),
                                         Price=100
                                     },
                                      new Product{
                                      Count=2,
                                       Name="产品2",
                                        CreateTime=new DateTime(2021,1,3),
                                         Price=500
                                     },
                                       new Product{
                                      Count=3,
                                       Name="产品3",
                                        CreateTime=new DateTime(2021,1,5),
                                         Price=600
                                     },
                                        new Product{
                                      Count=200,
                                       Name="产品4",
                                        CreateTime=new DateTime(2021,1,4),
                                         Price=100
                                     },
                                         new Product{
                                      Count=5,
                                       Name="产品5",
                                        CreateTime=new DateTime(2021,1,10),
                                         Price=300
                                     }
             };


            var data = products.OrderBy(ob => ob.CreateTime, new DateTimeComparer());
            Console.WriteLine("===================================");
            foreach (var item in data)
            {
                Console.WriteLine($"{item.Name},生产日期:{item.CreateTime},数量:{item.Count},价格:{item.Price}");
            }


            var data2 = products.OrderBy(q => q.CreateTime);

            Console.WriteLine("===================================");
            foreach (var item in data2)
            {
                Console.WriteLine($"{item.Name},生产日期:{item.CreateTime},数量:{item.Count},价格:{item.Price}");
            }
        }
        class Product
        {
            public int Count { get; set; }
            public string  Name { get; set; }
            public DateTime CreateTime { get; set; }
            public decimal Price { get; set; }

        }

        class ProductComparer : IComparer<Product>
        {
            public int Compare([AllowNull] Product x, [AllowNull] Product y)
            {
                if (x.CreateTime.CompareTo(y.CreateTime) != 0)
                {
                    return x.CreateTime.CompareTo(y.CreateTime);
                }
                else if (x.Count.CompareTo(y.Count) != 0)
                {
                    return x.Count.CompareTo(y.Count);
                }
                else
                {
                    return 0;
                }

            }
        }

        class DateTimeComparer : IComparer<DateTime>
        {
            public int Compare([AllowNull] DateTime x, [AllowNull] DateTime y)
            {
                if (x > y)
                {
                    return -1;
                }
                else if (x < y)
                {
                    return 1;
                }
                else
                {
                    return 0;
                }
            }
        }

 测试。。。

static  void  Main(string[] args)
{
 
             //OrderByOperator.Dome1();
             OrderByOperator.Dome2();
            
            System.Console.WriteLine();
            Console.ReadKey();
}

 

C# Linq 之 OrderBy

上一篇:UE4_Plugins模板Editor Standalone Window 初步理解


下一篇:Windows 搭建 hbase 单机版