通过写一个Demo展示C#中多种常用的集合排序方法
2017-04-10 17:04
316 查看
不多说,程序很简单,就是将集合中的数据进行排序,但使用到的知识点还是比较多的,大牛勿喷,谨献给初学者!直接上程序吧!
namespace Demo { /// <summary> /// Demo:使用不同排序方法对元素进行排序 /// </summary> class Program { private static void Main(string[] args) { ArrayList arrayList = Product.GetArrayList(); List<Product> list = Product.GetList(); //1、使用继承IComparer接口的函数来进行排序 arrayList.Sort(new ProductCompare()); foreach (Product product in arrayList) { Console.WriteLine(product.ToString()); } Console.WriteLine("---------------------------"); //2、使用继承IComparer<T>接口的函数来进行排序 list.Sort(new ProductCompareT()); foreach (Product product in list) { Console.WriteLine(product.ToString()); } Console.WriteLine("---------------------------"); //3、使用委托来进行排序 list.Sort(delegate(Product x, Product y) { return x.Price.CompareTo(y.Price); }); foreach (Product product in list) { Console.WriteLine(product.ToString()); } //4、使用Lambda表达式来进行排序; list.Sort((x, y) => x.Price.CompareTo(y.Price)); foreach (Product product in list) { Console.WriteLine(product.ToString()); } //5、使用扩展方法来进行排序 foreach (Product product in list.OrderBy(p=>p.Price)) { Console.WriteLine(product.ToString()); } Console.ReadKey(); } } public class Product { public string Name { get; set; } public decimal Price { get; set; } public static ArrayList GetArrayList() { return new ArrayList() { new Product {Name = "WindowsPhone", Price = 10m}, new Product {Name = "Apple", Price = 20m}, new Product {Name = "Android", Price = 5m} }; } public static List<Product> GetList() { return new List<Product>() { new Product {Name = "WindowsPhone", Price = 10m}, new Product {Name = "Apple", Price = 20m}, new Product {Name = "Android", Price = 5m} }; } public override string ToString() { return String.Format("{0}--{1}", Name, Price); } } /// <summary> /// 使用IComparer对ArrayList进行排序 /// 显示实现Compare接口,常用ArrayList类型的集合来调用 /// </summary> public class ProductCompare : IComparer { public int Compare(object x, object y) { Product first = x as Product; Product second = y as Product; if (first != null && second !=null) { return first.Price.CompareTo(second.Price); } else { return -1; } } } /// <summary> /// 使用IComparer<Product>进行排序 /// 显式实现Compare接口,常用List<T>类型的集合来调用 /// </summary> public class ProductCompareT : IComparer<Product> { public int Compare(Product x, Product y) { Product first = x as Product; Product second = y as Product; if (first != null && second != null) { return first.Price.CompareTo(second.Price); } else { return -1; } } } }
相关文章推荐
- 通过写一个Demo展示C#中多种常用的集合排序方法
- 判断一个字符串是否全是数字的多种方法及其性能比较(C#实现)
- C# ASP.NET 最常用的通用权限的3个方法例子展示(每个功能一行代码实现)
- C# ASP.NET 最常用的通用权限的3个方法例子展示(每个功能一行代码实现)
- 几种常用排序方法的C#实现
- 常用排序方法(C#)
- 逐步为对象集合构建一个通用的按指定属性排序的方法
- C#&.net常用方法集合
- 几种常用的C#排序方法简介
- 收藏的一个关于C# ToString的方法集合
- C#基础:通过一个实例来学习IFormattable接口,扩展方法
- 判断一个字符串是否全是数字的多种方法及其性能比较(C#实现)
- 判断一个字符串是否全是数字的多种方法及其性能比较(C#实现)
- 几种常用的C#排序方法简介
- .NET/C#中对自定义对象集合进行自定义排序的方法
- 判断一个字符串是否全是数字的多种方法及其性能比较(C#实现)
- .NET/C#中对自定义对象集合进行自定义排序的方法
- C# ASP.NET 最常用的通用权限的3个方法例子展示(每个功能一行代码实现)
- 收藏的一个关于C# ToString的方法集合
- (转)逐步为对象集合构建一个通用的按指定属性排序的方法