C#实现Comparable接口实现排序
C#中,实现排序的方法有两种,即实现Comparable或Comparer接口,下面简单介绍实现Comparable接口实现排序功能。
实现Comparable接口需要实现CompareTo(object obj)方法,所以简单实现这个方法就可以很方便的调用其排序功能。
以Student的score为例,进行排序:
具体代码:
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace ConsoleApplicationtest
- {
- class Program
- {
- public class Student:IComparable
- {
- int _socre;
- string _name;
- public int score
- {
- set { _socre = value; }
- get { return _socre; }
- }
- public string name
- {
- set { _name = value; }
- get { return _name; }
- }
- public void Display()
- {
- Console.WriteLine("姓名:{0}\t分数:{1}",_name,_socre);
- }
- public int CompareTo(object obj)//实现接口
- {
- Student stu = (Student)obj;
- return this._socre - stu._socre;
- }
- }
- static void Main(string[] args)
- {
- List<Student> stuList = new List<Student>();
- stuList.Add(new Student() { score = 98, name = "Bob" });
- stuList.Add(new Student() { score = 56, name = "Alice" });
- stuList.Add(new Student() { score = 100, name = "Jerry" });
- Console.WriteLine("排序前:");
- foreach (Student mystu in stuList)
- {
- mystu.Display();
- }
- stuList.Sort();
- Console.WriteLine("\n排序后:");
- foreach (Student mystu in stuList)
- {
- mystu.Display();
- }
- Console.ReadKey();
- }
- }
- }
C#实现Comparable接口实现排序的更多相关文章
- Java基础 TreeSet()来实现数组的【定制排序】 : Comparable接口(自然排序) 或者 Comparator接口 (定制排序)
笔记: //排序真麻烦!没有C++里的好用又方便!ORZ!ORZ!数组排序还还自己写个TreeSet()和( Comparable接口(自然排序) 或者 Comparator接口 (定制排序))imp ...
- 使用Comparable接口自定义排序
Employee: package textq; /** * 调用接口Comparable排序 * @author Administrator * */ public class Employee i ...
- java中的类实现comparable接口 用于排序
import java.util.Arrays; public class SortApp { public static void main(String[] args) { Student[] s ...
- 关于Comparable接口的使用
一.使用Comparable接口进行排序:如何要都某种数据类型或者是自定义的类进行排序必须要实现Comparable jdk定义的基本数据类型和String类型的数据都实现了Comparable.下面 ...
- Java 之 比较器( Comparator接口与 Comparable 接口)
一.定制排序:java.util.Comparator 接口 强行对某个对象 collection 进行整体排序 的比较函数.可以将 Comparator 传递给 sort 方法(如 Collecti ...
- Comparable接口与Comparator接口的比较————总结
之前的两篇文章主要学习了Comparable接口和Comparator接口的学习.既然已经学习完了,现在就趁热打铁,进行总结吧! Comparable接口和Comparator接口的共同点: 1. 都 ...
- Comparable 接口与Comparator的使用的对比
package com.yhqtv.java; import org.junit.Test; import java.util.Arrays; import java.util.Comparator; ...
- 通过实现Comparable接口结合TreeSet来对对象自动排序
经过会遇到这样的情况,对于某个对象数组或者链表要按照一定的规则进行排序,那么我们该怎么做呢? 如遇到这样的需求: 1.需求1 对于学生对象按照年龄进行排序,年龄小的排在前面. 单单看到这样的需求,实现 ...
- JAVA排序(一) Comparable接口
昨天接到一个实习公司的电话面试,来的很突然,没有准备. 由于以前没用过,在被他问及是否用过JAVA的排序工具Comparable与Comparator时,没有回答上来,只能实话实说没有用过. 感觉太丢 ...
随机推荐
- linux 压缩文件 及压缩选项详解
本文介绍linux下的压缩程序tar.gzip.gunzip.bzip2.bunzip2.compress.uncompress. zip. unzip.rar.unrar等程式,以及如何使用它们对. ...
- 【F#】 WebSharper框架
WebSharper,它是一个基于F#构建的Web开发平台,使用F#构造从前到后的一整套内容.其中利用到F#中许多高级的开发特性,并可以将F#代码直接转化JavaScript,这样服务器端和客户端的通 ...
- sql2008还原问题
2003的服务器系统不知是因为对SSD的支持不好还是别的原因,系统使用有很多奇怪的问题,所以就升级一下系统,在备份数据库的时候出现了各种问题 源数据库与现数据库都为sqlserver2008,并都安装 ...
- Document Set 【一】
概括介绍: Document Set 是SharePoint2010之后出现的一个新的Feature.这个Feature的主要目的是两个: 1,是帮助 User 以一个文件的管理方式管理一个文件集合. ...
- 用两个栈来实现一个队列,完成队列的Push和Pop操作。 队列中的元素为int类型。
// test14.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include<iostream> #include< ...
- C# 客户端判断服务器连接已断开
问题描述: 在C# Socket编程中,服务器端已经断开连接(发送数据方),客户端接收服务器端发送数据,在客户端使用client.Recieve()中,服务器端断开连接,客户端任然显示已 ...
- 【BZOJ】【2132】圈地计划
网络流/最小割 Orz Hzwer 这类大概是最小割建模中的经典应用吧…… 黑白染色,然后反转黑色的技巧感觉很巧妙!这个转化太神奇了…… /****************************** ...
- pku ppt some problem
The Triangle http://poj.org/problem?id=1163 暴力dfs的话,每个节点有两条路可以走,那么n个节点复杂度就是2^n n=100 超时 dp来做 就优 ...
- C#中Hashtable容器的了解与使用
初涉Hashtable寄语 由于近段时间培训内容涉及到Hashtable方面的知识,由于培训仅仅起到一个引导的作用,加之以前又接触得少,因此对Hashtable这个东东蛮陌生,呵呵,今晚木有事儿就一起 ...
- 【C++基础】指针好难啊,一点点啃——基本概念
指针保存的是另一个对象的地址(概念真的很重要!!) ; int *ptr = &a;//*定义一个指向int类型的指针ptr, &a取变量a的地址 引用是对象的别名,多用于函数形参,引 ...