DateTimeComparer

public int Compare(string x,string y)
{
DateTime xDate = DateTime.ParseExact(x, "MMMM", new CultureInfo("en-US"));
DateTime yDate = DateTime.ParseExact(y, "MMMM", new CultureInfo("en-US"));
return (Comparer<DateTime>.Default.Compare(xDate, yDate));
}

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Globalization;

namespace ConsoleApp84
{
class MonthComparer:IComparer<string>
{
public int Compare(string x,string y)
{
DateTime xDate = DateTime.ParseExact(x, "MMMM", new CultureInfo("en-US"));
DateTime yDate = DateTime.ParseExact(y, "MMMM", new CultureInfo("en-US"));
return (Comparer<DateTime>.Default.Compare(xDate, yDate));
}
}
public enum Countries
{
USA,
Italy
}

public class Order
{
public int IdOrder;
public int Quantity;
public bool Shipped;
public string Month;
public int IdProduct;

public override string ToString()
{
return string.Format("IdOrder: {0} -IdProduct:{1}- " + " Quantity:{2}- Shipped:{3} -" + " Month:{4}",
this.IdOrder, this.IdProduct, this.Quantity, this.Shipped, this.Month);
}
}

public class Product
{
public int IdProduct;
public decimal Price;

public override string ToString()
{
return string.Format("IdProduct:{0}-Price:{1}", this.IdProduct, this.Price);
}
}
public class Customer
{
public string Name;
public string City;
public Countries Country;
public Order[] Orders;

public override string ToString()
{
return string.Format("Name:{0}- City:{1}- Country:{2}", this.Name, this, City, this.Country);
}

}
class Program
{
static void Main(string[] args)
{
var customers = new Customer[]
{
new Customer {Name = "Paolo", City = "Brescia",Country = Countries.Italy, Orders = new Order[] {new Order { IdOrder = 1, Quantity = 3, IdProduct = 1 ,
Shipped = false, Month = "January"},new Order { IdOrder = 2, Quantity = 5, IdProduct = 2 ,Shipped = true, Month = "May"}}},
new Customer {Name = "Marco", City = "Torino",Country = Countries.Italy, Orders = new Order[] {new Order { IdOrder = 3, Quantity = 10, IdProduct = 1 ,
Shipped = false, Month = "July"},new Order { IdOrder = 4, Quantity = 20, IdProduct = 3 ,Shipped = true, Month = "December"}}},
new Customer {Name = "James", City = "Dallas",Country = Countries.USA, Orders = new Order[] {new Order { IdOrder = 5, Quantity = 20, IdProduct = 3 ,
Shipped = true, Month = "December"}}},
new Customer {Name = "Frank", City = "Seattle",Country = Countries.USA, Orders = new Order[] {new Order { IdOrder = 6, Quantity = 20, IdProduct = 5 ,
Shipped = false, Month = "July"}}}};

var products = new Product[] {new Product {IdProduct = 1, Price = 10 },new Product {IdProduct = 2, Price = 20 },new Product {IdProduct = 3, Price = 30 },
new Product {IdProduct = 4, Price = 40 },new Product {IdProduct = 5, Price = 50 },new Product {IdProduct = 6, Price = 60 }};

int start = 5, end = 10;

var orders = customers
.SelectMany(x => x.Orders)
.OrderBy(x => x.Month, new MonthComparer());
foreach(var order in orders)
{
Console.WriteLine(order);
}
Console.ReadLine();
}
}
}

原文链接: https://www.cnblogs.com/Fred1987/p/6380045.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍;

也有高质量的技术群,里面有嵌入式、搜广推等BAT大佬

    DateTimeComparer

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/402255

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年4月19日 上午9:40
下一篇 2023年4月19日 上午9:40

相关推荐