List集合操作二:排序
本文继续讲解List<T>之排序操作,C#范型List类的Sort方法有四种形式,分别是:
1、不带有任何参数的Sort方法----Sort();
2、带有比较器参数的Sort方法 ----Sort(IComparer<T>)
3、带有比较代理方法参数的Sort方法----Sort(Comparison<(Of <(T>)>))
4、带有比较起参数,可以指定排序范围的Sort方法----Sort(Int32, Int32 IComparer(T))
首先对IComparable.CompareTo 方法进行一下介绍:
MSDN解释:将当前实例与同一类型的另一个对象进行比较,并返回一个整数,该整数指示当前实例在排序顺序中的位置是位于另一个对象之前、之后还是与其位置相同。
原型:int CompareTo (Object obj)
参数 obj:与此实例进行比较的对象。
返回值:一个 32 位有符号整数,指示要比较的对象的相对顺序。
返回值的含义如下:
小于0 此实例小于 obj;位置在obj之前。
等于0 此实例等于 obj;位置在obj相同。
大于0 此实例大于 obj;位置在obj之后。
下面我们通过一个控制台实例来分别介绍这四种方法:
第一种方法元素继承IComparable:
* 使用这种方法不是对List中的任何元素对象都可以进行排序
* List中的元素对象必须继承IComparable接口并且要实现IComparable接口中的CompareTo()方法
* 在CompareTo()方法中要自己实现对象的比较规则
我们还沿用第一篇的实体类Player 并使其继承IComparable接口
using System;namespace ListDemo2 {public class Player : IComparable<Player>{public Player(int id, string name, string team){this.Id = id;this.Name = name;this.Team = team;}public int Id { get; set; }public string Name { get; set; }public string Team { get; set; }#region IComparable<Model> 成员/// <summary>/// 实现的IComparable接口,用于进行比较。因为排序是建立在比较的基础之上的。/// </summary>/// <param name="otherP">另外一个Player</param>/// <returns>/// 小于0 此实例按排序顺序在 otherP 前面/// 等于0 此实例与otherP 在排序顺序中出现的位置相同/// 大于0 此实例按排序顺序在 otherP 后面/// </returns>public int CompareTo(Player otherP){//只要调换位置就可以调整排序的方式//return this.Id.CompareTo(otherP.Id); //正序return otherP.Id.CompareTo(this.Id); //倒序 }#endregion} }
定义一个集合类PlayerList
using System.Collections.Generic;namespace ListDemo2 {public class PlayerList : List<Player>{public PlayerList(){this.Add(new Player(1, "科比-布莱恩特", "湖人队"));this.Add(new Player(2, "保罗-加索尔", "湖人队"));this.Add(new Player(3, "拉玛尔-奥多姆", "湖人队"));this.Add(new Player(4, "德克-诺维茨基", "小牛队"));this.Add(new Player(5, "杰森-特里", "小牛队"));this.Add(new Player(6, "肖恩-马里昂", "小牛队"));this.Add(new Player(7, "凯文-加内特", "凯尔特人队"));}} }
Main函数代码:
using System; using System.Collections.Generic;namespace ListDemo2 {class Program{static void Main(string[] args){//1、不带有任何参数的Sort方法PlayerList players = new PlayerList();Action<Player> listSort = delegate(Player p){Console.WriteLine(string.Format("队员Id={0} | 队员名称={1} | 所属球队={2} ", p.Id, p.Name, p.Team));};Console.ForegroundColor = ConsoleColor.Red;Console.WriteLine("第一种不带有任何参数的Sort方法");Console.WriteLine("实现元素继承ICompare实现_队员ID从大到小排列:");Console.ForegroundColor = ConsoleColor.Yellow;players.Sort();players.ForEach(listSort);Console.ReadKey();}} }
执行结果如下图:
第二种带有比较器参数的Sort方法
* List中的元素对象不需要继承IComparable接口
* 但需要额外创建一个对象的比较器
我们重新定义个Student 实体类:
namespace ListDemo2 {public class Student{public Student(int id, string name, float score){this.Id = id;this.Name = name;this.Score = score;}public int Id { get; set; }public string Name { get; set; }public float Score { get; set; }}
再定义个StudentList集合类:
using System.Collections.Generic;namespace ListDemo2 {public class StudentList : List<Student>{public StudentList(){this.Add(new Student(1, "小明", 88.5f));this.Add(new Student(2, "小红", 90));this.Add(new Student(3, "小兰", 93));this.Add(new Student(4, "小花", 72.5f));this.Add(new Student(5, "小猫", 88));this.Add(new Student(6, "小狗", 63));}} }
我们定义一个StudentCompare类继承 IComparer接口:
using System; using System.Collections.Generic;namespace ListDemo2 {public class StudentCompare : IComparer<Student>{//比较器的枚举,可以按照Id 和 Score 进行排序public enum CompareType { Id, Score }private CompareType compareType;public StudentCompare(CompareType compareType){this.compareType = compareType;}public int Compare(Student x, Student y){if ((x == null) || (y == null))return -1;switch (compareType){case CompareType.Id:return x.Id.CompareTo(y.Id);//成绩从大到小排列case CompareType.Score:return y.Score.CompareTo(x.Score);default:return -1;}}} }
Main函数代码:
using System; using System.Collections.Generic;namespace ListDemo2 {class Program{static void Main(string[] args){ //2、带有比较器参数的Sort方法 ---Sort(IComparer<T>) StudentList students1 = new StudentList();Console.ForegroundColor = ConsoleColor.Red;Console.WriteLine("第二种带有比较器参数的Sort方法");Console.WriteLine("实现_学生成绩从大到小排列:");Console.ForegroundColor = ConsoleColor.Yellow;students1.Sort(new StudentCompare(StudentCompare.CompareType.Score));students1.ForEach(s => Console.WriteLine(string.Format("Id={0} | 姓名={1} | 成绩={2} ", s.Id, s.Name, s.Score)));Console.ReadKey();}} }
执行结果如下图:
第三种方法需要编写一个对象排序比较的方法
* 对List中的元素对象没有特殊的要求
* 但在比较方法中需要实现对象比较规则
* 这个方法实现后,就可以把这方名字作为参数委托给List的Sort方法
* Sort方法在排序时会执行这个方法对List中的对象进行比较
我们写个为学生成绩从小到大排序的比较方法:
private static int SortStudentCompare(Student stu1, Student stu2) {return stu1.Score.CompareTo(stu2.Score); }
Main 函数代码:
using System; using System.Collections.Generic;namespace ListDemo2 {class Program{static void Main(string[] args){ //3、带有比较代理方法参数的Sort方法StudentList student2 = new StudentList();Console.ForegroundColor = ConsoleColor.Red;Console.WriteLine("带有比较代理方法参数的Sort方法");Console.WriteLine("实现_学生成绩从小到大排列:");Console.ForegroundColor = ConsoleColor.Yellow;student2.Sort(SortStudentCompare);student2.ForEach(s => Console.WriteLine(string.Format("Id={0} | 姓名={1} | 成绩={2} ", s.Id, s.Name, s.Score)));Console.ReadKey();}} }
执行结果如图:
第四种方法其实是一种扩展,可以对List内部分数据进行排序
比如我们可以为StudentList的前三名学生的成绩进行从大到小的排序:
using System; using System.Collections.Generic;namespace ListDemo2 {class Program{static void Main(string[] args){//4、带有比较起参数,可以指定排序范围的Sort方法StudentList student3 = new StudentList();Console.ForegroundColor = ConsoleColor.Red;Console.WriteLine("带有比较起参数,可以指定排序范围的Sort方法");Console.WriteLine("实现_前3名学生成绩从大到小排列:");Console.ForegroundColor = ConsoleColor.Yellow;student3.Sort(0, 3, new StudentCompare(StudentCompare.CompareType.Score));student3.ForEach(s => Console.WriteLine(string.Format("Id={0} | 姓名={1} | 成绩={2} ", s.Id, s.Name, s.Score)));Console.ReadKey();}} }
执行结果如下图:
-=源码下载=-
转载于:https://www.cnblogs.com/lxblog/archive/2012/08/23/2652859.html
List集合操作二:排序相关推荐
- [转载] 05 Numpy排序搜索计数及集合操作
参考链接: Numpy 排序,搜索和计数 排序,搜索和计数 排序 numpy.sort() numpy.sort(a[, axis=-1, kind='quicksort', order=None]) ...
- 1.8 Collections类操作集合详解——排序,查找,复制
Collections类操作集合详解 Collections 类是 Java 提供的一个操作 Set.List 和 Map 等集合的工具类. Collections 类提供了许多操作集合的静态方法,借 ...
- 好用java库(二) : lambdaj (集合操作)
接着介绍另外一个好用的java库. 记得之前做过一个web services,业务逻辑是很简单,可是代码写得多又长,因为基本上都是在对ArrayList结果进行各种筛选,排序,聚合等操作.大家都有这样 ...
- TreeSet集合(自然排序和比较器排序)
TreeSet集合 自然排序和比较器排序 当指执行插入排序.希尔排序.归并排序等算法时,比较两个对象"大小"的比较操作.我们很容易理解整型的 i>j 这样的比较方式,但当 ...
- Numpy入门教程:08. 集合操作
背景 什么是 NumPy 呢? NumPy 这个词来源于两个单词 – Numerical和Python.其是一个功能强大的 Python 库,可以帮助程序员轻松地进行数值计算,通常应用于以下场景: 执 ...
- Oracle 语言分类 数据类型 数据类型转换 常用函数 集合操作 子查询
SQL分类 SQL(Structure Query Language)语言是数据库的核心语言.SQL语言共分为四大类:数据定义语言DDL,数据操纵语言DML,数据查询语言DQL,数据控制语言DCL.1 ...
- android list 替换元素_Java 集合(二)——Set 集合、List 集合和 Collections 工具类...
一.前言 在 Java 集合(一)中我们已经讲了 Collection 集合接口.Iterator 迭代器和泛型,今天我们来讲 Set 集合.List 集合 和 Collections 工具类. 二. ...
- 关于JDK中的集合总结(二)
1.2版本的JDK才出现的java集合框架. 下面介绍说一下Vector的一些特点. 1 import java.util.Enumeration; 2 import java.util.Iterat ...
- OBIEE使用”集合操作”完成复杂格式报表
我们通过以下面这个例子来讲解如何使用BIEE的"集合操作"来完成复杂格式的报表. 例子中的这张报表情景来源于实际项目案例,我们在这里直接使用BIEE自带paint来进行模拟. 一. ...
- 集合概述二(Set接口+HashSet集合+LinkedHashSet集合+TreeSet集合)
一(Set接口): 1.Set接口和List接口一样,同样继承自Collection接口,它与Collection接口中的方法基本一致,并没有对Collection接口进行功能上的扩充,只是比Coll ...
最新文章
- MySQL外键关联(一对多)MySQL连接查询
- (转载) Linux IO模式及 select、poll、epoll详解
- python文件操作小总结
- Redis集群之哨兵模式
- 勇敢一次_开放网络需要勇敢的新英雄吗?
- 访问订单列表时报错404
- JSTL标签用法:c:choosec:forEachc:ifc:whenc:set
- LTE学习:PDCCH信道
- 如何从asio::udp::socket()中取出底层sockfd整数
- 使用localResizeIMG3+WebAPI实现手机端图片上传
- 平面2R机器人的运动学/动力学建模实例
- 模拟集成电路设计初学系列
- pdfjs转图片_PDF转图片,PDF转JPG/PNG,完全由JS实现-阿里云开发者社区
- [密码学复习]Cryptography
- ibm tivoli_使用Tivoli®Composite Application Manager监视Tivoli®Access Manager WebSEAL服务器事务以进行响应时间跟踪
- 计算机vb里代码里的双引号,在VB中使用字符串中的左双引号
- 视频编码第一节:H.265/HEVC原理——入门
- 首席新媒体商学院黎想:全新的抖音涨粉攻略
- Linux-打包、压缩命令
- 仿简书登录框,可删除内容或显示密码框的内容
热门文章
- PHP 删除文件,文件下的目录
- PHP trim()的使用
- linux camera 存储,OpenCV调用摄像头录像并保存下来
- hadoop命令使用put上传文件报错
- linux 程序 指定网卡,Linux socket绑定指定网卡实现负载均衡
- 全网首发:There is an incompatible JNA native library installed on this system/6.1.2/4.0.1
- 用了几个开源项目,都无法运行,推广柳氏风格
- 制作CDKEY:有效期的处理
- 明明是OS问题,却认为是CPU,这个教训是什么
- VS log4net引用错误的解决