不多说,程序很简单,就是将集合中的数据进行排序,但使用到的知识点还是比较多的,大牛勿喷,谨献给初学者!直接上程序吧!

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 -;
}
}
} /// <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 -;
}
}
}
}

通过写一个Demo展示C#中多种常用的集合排序方法的更多相关文章

  1. 【跟着子迟品 underscore】如何优雅地写一个『在数组中寻找指定元素』的方法

    Why underscore (觉得这部分眼熟的可以直接跳到下一段了...) 最近开始看 underscore.js 源码,并将 underscore.js 源码解读 放在了我的 2016 计划中. ...

  2. Java基础-接口.编写2个接口:InterfaceA和InterfaceB;在接口InterfaceA中有个方法void printCapitalLetter();在接口InterfaceB中有个方法void printLowercaseLetter();然 后写一个类Print实现接口InterfaceA和InterfaceB,要求 方法 实现输出大写英文字母表的功能,printLowerca

    #34.编写2个接口:InterfaceA和InterfaceB:在接口InterfaceA中有个方法void printCapitalLetter():在接口InterfaceB中有个方法void ...

  3. C#中遍历各类数据集合的方法总结

    C#中遍历各类数据集合的方法总结: 1.枚举类型 //遍历枚举类型Sample的各个枚举名称 foreach (string sp in Enum.GetNames(typeof(Sample))) ...

  4. 【.NET】C#中遍历各类数据集合的方法

    [.NET]C#中遍历各类数据集合的方法   C#中遍历各类数据集合的方法,这里自己做下总结: 1.枚举类型             //遍历枚举类型Sample的各个枚举名称             ...

  5. RxJava2 中多种取消订阅 dispose 的方法梳理( 源码分析 )

    Github 相关代码: Github地址 一直感觉 RxJava2 的取消订阅有点混乱, 这样也能取消, 那样也能取消, 没能系统起来的感觉就像掉进了盘丝洞, 迷乱… 下面说说这几种情况 几种取消的 ...

  6. Java中8种常见的排序方法

    排序方法的演示1)插入排序(直接插入排序.希尔排序)2)交换排序(冒泡排序.快速排序)3)选择排序(直接选择排序.堆排序)4)归并排序5)分配排序(基数排序)所需辅助空间最多:归并排序所需辅助空间最少 ...

  7. db2 查看进程 db2中的常用命令及使用方法

    一 高(重要度) 1 启动一个db 2实例使用: net start instanceName 2 停止一个db 2实例使用: net stop instanceName 3 启动配置助手: db2= ...

  8. Javascript中,实现十大排序方法之一(冒泡排序及其优化设想)

    冒泡排序的Javascript实现 首先定义一个取值范围在(0~100000)之间的随机值的长度为10万的数组, function bubbleSort(arr) { console.time('冒泡 ...

  9. JS中json数组多字段排序方法(解决兼容性问题)(转)

    前端对一个json数组进行排序,用户需要动态的根据自己的选择来对json数据进行排序. 由于后台表设计问题所以不能用sql进行排序,这里用到了js的sort方法. 如果对单字段排序,那么很简单,一个s ...

随机推荐

  1. Word中的段落

    Word文档中的块级内容的最基本单位是段落,段落用<p>元素进行存储.段落定义在新行中开始,段落可以包含三方面的信息:可选的段落属性.内嵌的内容(通常为文本)和用于比较两个文档的内容的一组 ...

  2. Java ,python面向对象的继承及其区别

    JAVA JAVA继承基本样式 class Demo extends Object{ Demo(int a){ this(); } Demo(){ super(); } } java默认继承Objec ...

  3. 《pyhton语言程序设计》_第一章笔记

    章1.62 (1).python区分大小写. (2).python忽略在符号#之后的同行的内容 (3).python和matlab很相似(个人感觉) (4).章节1.91: >>>i ...

  4. Servlet 中,out.print()与out.write()的区别

    最近刚学习了Ajax,其中有通过$.getJSON的实现方式 由于前后端传递值的时候会通过流的方式进行传递,这就不得不涉及到这方面的知识了 PrintWrite out=response.getWri ...

  5. 我为什么要选择RabbitMQ ,RabbitMQ简介,各种MQ选型对比(转载)

    转载自:https://www.sojson.com/blog/48.html 前言: MQ 是什么?队列是什么,MQ 我们可以理解为消息队列,队列我们可以理解为管道.以管道的方式做消息传递. 场景: ...

  6. CentOS第一次安装MySQL的完整步骤

    文章来自:http://www.jianshu.com/p/4a41a6df19a6,我自己调整了下 1.官方安装文档 http://dev.mysql.com/doc/mysql-yum-repo- ...

  7. Spring IOC容器的实现原理

    1 概述 1.1 依赖反转模式 在Java中,一个复杂的功能一般都需要由两个或者两个以上的类通过彼此合作来实现业务逻辑的,这使得每个对象都需要与其合作的对象的引用.如果这个获取依赖对象的过程需要自己去 ...

  8. spring boot log4j2配置

    [传送门]:log4j官网配置文件详解 1. 排除 spring boot 自带的  spring-boot-starter-logging 依赖 <dependency> <gro ...

  9. Swift 里 Set(五)Adding & Removing Elements

    Adding Elements internal func _unsafeInsertNew(_ element: __owned Element) { _internalInvariant(coun ...

  10. Django --需求文件requirements.txt

    在虚拟环境中使用pip生成: (venv) $ pip freeze >requirements.txt 当需要创建这个虚拟环境的完全副本,可以创建一个新的虚拟环境,并在其上运行以下命令: (v ...