码迷,mamicode.com
首页 > 其他好文 > 详细

DateTimeComparer

时间:2017-02-08 23:03:28      阅读:229      评论:0      收藏:0      [点我收藏+]

标签:system   country   price   tom   dal   tle   space   custom   sci   

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();
}
}
}

DateTimeComparer

标签:system   country   price   tom   dal   tle   space   custom   sci   

原文地址:http://www.cnblogs.com/Fred1987/p/6380045.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!