深入IComparable與IComparer的排序?qū)嵗斀?/h1>
更新時(shí)間:2013年06月09日 10:09:16 作者:
本篇文章是對(duì)IComparable與IComparer的排序?qū)嵗M(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
如下所示:
復(fù)制代碼 代碼如下:
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
namespace hgoApp
{
class Comparer
{
static void Main()
{
Employee[] Employees = new Employee[5];
Employees[0] = new Employee("張三", 2800);
Employees[1] = new Employee("李四", 1800);
Employees[2] = new Employee("王五", 5800);
Employees[3] = new Employee("馬六", 12800);
Employees[4] = new Employee("錢七", 8800);
Console.WriteLine("按照姓名排序");
Array.Sort(Employees);
foreach (Employee em in Employees)
{
Console.WriteLine(em);
}
Console.WriteLine("按照薪水排序");
Array.Sort(Employees, Employee.SalaryCom);
foreach (Employee em in Employees)
{
Console.WriteLine(em);
}
}
}
class Employee : IComparable
{
private string _Name;
public string Name
{
get { return _Name; }
}
private int _Salary;
public int Salary
{
get { return _Salary; }
}
public Employee(string Name, int Salary)
{
_Name = Name;
_Salary = Salary;
}
//顯示接口實(shí)現(xiàn)
int IComparable.CompareTo(object obj)
{
if (!(obj is Employee))
{
throw new ArgumentException("不是Employee類");
}
return _Name.CompareTo(((Employee)obj)._Name);
}
//提供一個(gè)公用的強(qiáng)類型重載版本
public int CompareTo(Employee Em)
{
return _Name.CompareTo(Em._Name);
}
//當(dāng)?shù)谝淮螌?shí)例化Employee的時(shí)候,_SalaryCom為空,當(dāng)使用SalaryCom時(shí),就會(huì)創(chuàng)建一個(gè)SalaryCompare對(duì)象,第2次,3次的時(shí)候,就可以直接使用_SalaryCom了
private static SalaryCompare _SalaryCom = null;
public static IComparer SalaryCom
{
get
{
if (_SalaryCom == null)
{
_SalaryCom = new SalaryCompare();
}
return _SalaryCom;
}
}
//嵌套類(這個(gè)類是針對(duì)薪水進(jìn)行排序的類)
private class SalaryCompare:IComparer
{
//使用指定的IComparer對(duì)Array中的元素進(jìn)行排序
int IComparer.Compare(object obj1,object obj2)
{
if (!(obj1 is Employee) || !(obj2 is Employee))
{
throw new ArgumentException("不是Employee類");
}
return ((Employee)obj1)._Salary.CompareTo(((Employee)obj2)._Salary);
}
}
public override string ToString()
{
return _Name +" "+ _Salary.ToString();
}
}
}
您可能感興趣的文章:- c# 實(shí)現(xiàn)IComparable、IComparer接口、Comparer類的詳解
- C#實(shí)現(xiàn)Datatable排序的方法
- C#中DataTable排序、檢索、合并等操作實(shí)例
- c#基數(shù)排序Radix sort的實(shí)現(xiàn)方法
- c#對(duì)list排序示例
- c#集合快速排序類實(shí)現(xiàn)代碼分享
- C#對(duì)DataTable里數(shù)據(jù)排序的方法
- C# 鍵值對(duì)數(shù)據(jù)排序代碼
- C#歸并排序的實(shí)現(xiàn)方法(遞歸,非遞歸,自然歸并)
- C#基礎(chǔ)之?dāng)?shù)組排序、對(duì)象大小比較實(shí)現(xiàn)代碼
- C#通過IComparable實(shí)現(xiàn)ListT.sort()排序
相關(guān)文章
-
C#中Override關(guān)鍵字和New關(guān)鍵字的用法詳解
這篇文章主要介紹了C#中Override關(guān)鍵字和New關(guān)鍵字的用法,需要的朋友可以參考下 2016-01-01
-
Linq兩個(gè)List集合取交集的實(shí)現(xiàn)
這篇文章主要介紹了Linq兩個(gè)List集合取交集的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧 2019-12-12
-
C# SQLite序列操作實(shí)現(xiàn)方法詳解
這篇文章主要介紹了C# SQLite序列操作實(shí)現(xiàn)方法,結(jié)合實(shí)例形式詳細(xì)分析了C#實(shí)現(xiàn)SQLite序列操作的具體步驟與相關(guān)操作技巧,需要的朋友可以參考下 2017-07-07
-
教你C#將CSV轉(zhuǎn)為Excel的實(shí)現(xiàn)方法
這篇文章主要介紹了C#?將CSV轉(zhuǎn)為Excel,轉(zhuǎn)換之后可執(zhí)行更多關(guān)于數(shù)據(jù)編輯、格式設(shè)置等操作,代碼簡單易懂,對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧 2022-03-03
-
C#使用XmlDocument或XDocument創(chuàng)建xml文件
這篇文章主要為大家詳細(xì)介紹了C#使用XmlDocument或XDocument創(chuàng)建xml文件,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下 2018-10-10
-
C#計(jì)算程序執(zhí)行過程花費(fèi)時(shí)間的方法
這篇文章主要介紹了C#計(jì)算程序執(zhí)行過程花費(fèi)時(shí)間的方法,涉及C#簡單的時(shí)間運(yùn)算技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下 2015-09-09
-
Unity3D基于UGUI實(shí)現(xiàn)虛擬搖桿
這篇文章主要為大家詳細(xì)介紹了Unity3D基于UGUI實(shí)現(xiàn)虛擬搖桿,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下 2020-04-04
最新評(píng)論
復(fù)制代碼 代碼如下:
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
namespace hgoApp
{
class Comparer
{
static void Main()
{
Employee[] Employees = new Employee[5];
Employees[0] = new Employee("張三", 2800);
Employees[1] = new Employee("李四", 1800);
Employees[2] = new Employee("王五", 5800);
Employees[3] = new Employee("馬六", 12800);
Employees[4] = new Employee("錢七", 8800);
Console.WriteLine("按照姓名排序");
Array.Sort(Employees);
foreach (Employee em in Employees)
{
Console.WriteLine(em);
}
Console.WriteLine("按照薪水排序");
Array.Sort(Employees, Employee.SalaryCom);
foreach (Employee em in Employees)
{
Console.WriteLine(em);
}
}
}
class Employee : IComparable
{
private string _Name;
public string Name
{
get { return _Name; }
}
private int _Salary;
public int Salary
{
get { return _Salary; }
}
public Employee(string Name, int Salary)
{
_Name = Name;
_Salary = Salary;
}
//顯示接口實(shí)現(xiàn)
int IComparable.CompareTo(object obj)
{
if (!(obj is Employee))
{
throw new ArgumentException("不是Employee類");
}
return _Name.CompareTo(((Employee)obj)._Name);
}
//提供一個(gè)公用的強(qiáng)類型重載版本
public int CompareTo(Employee Em)
{
return _Name.CompareTo(Em._Name);
}
//當(dāng)?shù)谝淮螌?shí)例化Employee的時(shí)候,_SalaryCom為空,當(dāng)使用SalaryCom時(shí),就會(huì)創(chuàng)建一個(gè)SalaryCompare對(duì)象,第2次,3次的時(shí)候,就可以直接使用_SalaryCom了
private static SalaryCompare _SalaryCom = null;
public static IComparer SalaryCom
{
get
{
if (_SalaryCom == null)
{
_SalaryCom = new SalaryCompare();
}
return _SalaryCom;
}
}
//嵌套類(這個(gè)類是針對(duì)薪水進(jìn)行排序的類)
private class SalaryCompare:IComparer
{
//使用指定的IComparer對(duì)Array中的元素進(jìn)行排序
int IComparer.Compare(object obj1,object obj2)
{
if (!(obj1 is Employee) || !(obj2 is Employee))
{
throw new ArgumentException("不是Employee類");
}
return ((Employee)obj1)._Salary.CompareTo(((Employee)obj2)._Salary);
}
}
public override string ToString()
{
return _Name +" "+ _Salary.ToString();
}
}
}
您可能感興趣的文章:
- c# 實(shí)現(xiàn)IComparable、IComparer接口、Comparer類的詳解
- C#實(shí)現(xiàn)Datatable排序的方法
- C#中DataTable排序、檢索、合并等操作實(shí)例
- c#基數(shù)排序Radix sort的實(shí)現(xiàn)方法
- c#對(duì)list排序示例
- c#集合快速排序類實(shí)現(xiàn)代碼分享
- C#對(duì)DataTable里數(shù)據(jù)排序的方法
- C# 鍵值對(duì)數(shù)據(jù)排序代碼
- C#歸并排序的實(shí)現(xiàn)方法(遞歸,非遞歸,自然歸并)
- C#基礎(chǔ)之?dāng)?shù)組排序、對(duì)象大小比較實(shí)現(xiàn)代碼
- C#通過IComparable實(shí)現(xiàn)ListT.sort()排序
相關(guān)文章
C#中Override關(guān)鍵字和New關(guān)鍵字的用法詳解
這篇文章主要介紹了C#中Override關(guān)鍵字和New關(guān)鍵字的用法,需要的朋友可以參考下2016-01-01
Linq兩個(gè)List集合取交集的實(shí)現(xiàn)
這篇文章主要介紹了Linq兩個(gè)List集合取交集的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12
C# SQLite序列操作實(shí)現(xiàn)方法詳解
這篇文章主要介紹了C# SQLite序列操作實(shí)現(xiàn)方法,結(jié)合實(shí)例形式詳細(xì)分析了C#實(shí)現(xiàn)SQLite序列操作的具體步驟與相關(guān)操作技巧,需要的朋友可以參考下2017-07-07
教你C#將CSV轉(zhuǎn)為Excel的實(shí)現(xiàn)方法
這篇文章主要介紹了C#?將CSV轉(zhuǎn)為Excel,轉(zhuǎn)換之后可執(zhí)行更多關(guān)于數(shù)據(jù)編輯、格式設(shè)置等操作,代碼簡單易懂,對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2022-03-03
C#使用XmlDocument或XDocument創(chuàng)建xml文件
這篇文章主要為大家詳細(xì)介紹了C#使用XmlDocument或XDocument創(chuàng)建xml文件,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-10-10
C#計(jì)算程序執(zhí)行過程花費(fèi)時(shí)間的方法
這篇文章主要介紹了C#計(jì)算程序執(zhí)行過程花費(fèi)時(shí)間的方法,涉及C#簡單的時(shí)間運(yùn)算技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-09-09
Unity3D基于UGUI實(shí)現(xiàn)虛擬搖桿
這篇文章主要為大家詳細(xì)介紹了Unity3D基于UGUI實(shí)現(xiàn)虛擬搖桿,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-04-04

