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接口实现排序的更多相关文章

  1. Java基础 TreeSet()来实现数组的【定制排序】 : Comparable接口(自然排序) 或者 Comparator接口 (定制排序)

    笔记: //排序真麻烦!没有C++里的好用又方便!ORZ!ORZ!数组排序还还自己写个TreeSet()和( Comparable接口(自然排序) 或者 Comparator接口 (定制排序))imp ...

  2. 使用Comparable接口自定义排序

    Employee: package textq; /** * 调用接口Comparable排序 * @author Administrator * */ public class Employee i ...

  3. java中的类实现comparable接口 用于排序

    import java.util.Arrays; public class SortApp { public static void main(String[] args) { Student[] s ...

  4. 关于Comparable接口的使用

    一.使用Comparable接口进行排序:如何要都某种数据类型或者是自定义的类进行排序必须要实现Comparable jdk定义的基本数据类型和String类型的数据都实现了Comparable.下面 ...

  5. Java 之 比较器( Comparator接口与 Comparable 接口)

    一.定制排序:java.util.Comparator 接口 强行对某个对象 collection 进行整体排序 的比较函数.可以将 Comparator 传递给 sort 方法(如 Collecti ...

  6. Comparable接口与Comparator接口的比较————总结

    之前的两篇文章主要学习了Comparable接口和Comparator接口的学习.既然已经学习完了,现在就趁热打铁,进行总结吧! Comparable接口和Comparator接口的共同点: 1. 都 ...

  7. Comparable 接口与Comparator的使用的对比

    package com.yhqtv.java; import org.junit.Test; import java.util.Arrays; import java.util.Comparator; ...

  8. 通过实现Comparable接口结合TreeSet来对对象自动排序

    经过会遇到这样的情况,对于某个对象数组或者链表要按照一定的规则进行排序,那么我们该怎么做呢? 如遇到这样的需求: 1.需求1 对于学生对象按照年龄进行排序,年龄小的排在前面. 单单看到这样的需求,实现 ...

  9. JAVA排序(一) Comparable接口

    昨天接到一个实习公司的电话面试,来的很突然,没有准备. 由于以前没用过,在被他问及是否用过JAVA的排序工具Comparable与Comparator时,没有回答上来,只能实话实说没有用过. 感觉太丢 ...

随机推荐

  1. Unable to create the store directory. (Exception from HRESULT: 0x80131468)

    一个ASP.NET的程序,使用了MS ReportViewer报告控件,在用该控件导出生成Excel文件时,先是提示行不能超过65535. 这个是由于Excel2003的行限制的原因.由于修改成用Ex ...

  2. Google protobuf安装

    1:需要安装sudo apt-get install x11-apps libwayland-ltst-client0 libtxc-dxtn-s2tc0 x11-session-utils  x11 ...

  3. Eclipse C/C++开发环境搭建

    1 Eclipse的安装 到http://java.sun.com/j2se/1.5.0/download.jsp 下载JRE安装: 到http://eclipse.org下载Eclipse安装.(这 ...

  4. php的public、protected、private三种访问控制模式的区别

    public: 公有类型 在子类中可以通过self::var调用public方法或属性,parent::method调用父类方法 在实例中可以能过$obj->var 来调用 public类型的方 ...

  5. ORACLE 变量定义

    DECLARE v_productid productinfo.productid%TYPE; v_productname ); v_productprice ,); v_quantity ); v_ ...

  6. android退出登陆后,清空之前所有的activity,进入登陆主界面

    如题: android退出登陆后,清空之前所有的activity,进入登陆主界面 在退出登陆时只需要增加一个intent标志 Intent intent_login = new Intent(); i ...

  7. [搜片神器]使用C#实现DHT磁力搜索的BT种子后端管理程序+数据库设计(开源)

    谢谢园子朋友的支持,已经找到个VPS进行测试,国外的服务器:http://www.sosobta.com   大家可以给提点意见... 出售商业网站代码,万元起,非诚勿扰,谢谢. 联系h31h31 a ...

  8. C#WinForm中在dataGridView中添加中文表头

    第一步: 注意事项:(1)如果使用数据库,那么第三步的名称可以是任意的,但是不能和数据库中的列名一样,否则会报错:    (2)第四步的页眉文本就是你想用的中文列名,自己定: (3)第六步尤其重要,不 ...

  9. 输入n个整数,找出其中最小的K个数。例如输入4,5,1,6,2,7,3,8这8个数字,则最小的4个数字是1,2,3,4,。

    // ConsoleApplication2.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include "stdafx.h ...

  10. 16进制串hex与ASCII字符串相互转换

    提供两个函数,方便十六进制串与ASCII 字符串之间的相互转换,使用函数需要注意的是返回的串是在堆上通过 calloc 分配的,所以,记得使用完返回值释放该块,并且将指向该块的指针 =NULL . c ...