泛型IComparer<T>排序
class Program
{
static void Main(string[] args)
{
GetListTest();
} private static void GetListTest()
{
DBHelper dbHelper = DBHelper.GetInstance();
DataSet ds = dbHelper.GetSqlDataSet("SELECT name,age FROM tbl_test",null); List<Person> listPerson = new List<Person>();
for (int i = ; i < ds.Tables[].Rows.Count; i++)
{
Person model = new Person();
model.Name = ds.Tables[].Rows[i]["name"].ToString();
model.Age = ds.Tables[].Rows[i]["age"].ToString();
listPerson.Add(model);
} //年龄排序
Console.WriteLine("----年龄排序----");
listPerson.Sort(new SortAge());
for (int i = ; i < listPerson.Count; i++)
{
Console.WriteLine(listPerson[i].Name + ":" + listPerson[i].Age);
}
Console.WriteLine("");
//姓名排序
Console.WriteLine("----姓名排序----");
listPerson.Sort(new SortName());
for (int i = ; i < listPerson.Count; i++)
{
Console.WriteLine(listPerson[i].Name + ":" + listPerson[i].Age);
}
} } /// <summary>
/// 排序实体
/// </summary>
class Person
{
private string name;
/// <summary>
/// 姓名
/// </summary>
public string Name
{
get { return name; }
set { name = value; }
} private string age;
/// <summary>
/// 年龄
/// </summary>
public string Age
{
get { return age; }
set { age = value; }
}
} /// <summary>
/// 年龄排序
/// </summary>
class SortAge :IComparer<Person>
{
public int Compare(Person x, Person y)
{
return x.Age.CompareTo(y.Age);
}
} /// <summary>
/// 姓名排序
/// </summary>
class SortName : IComparer<Person>
{
public int Compare(Person x, Person y)
{
return x.Name.CompareTo(y.Name);
}
}
泛型IComparer<T>排序的更多相关文章
- 泛型List<T>排序(利用反射)
在最近一个项目中,有需求要对页面中所有的gridview添加排序功能.由于gridview的数据源绑定的是一个集合类List,而不是DataTable,所以无法使用DataView排序功能.另外,不同 ...
- wpf 导出Excel Wpf Button 样式 wpf简单进度条 List泛型集合对象排序 C#集合
wpf 导出Excel 1 private void Button_Click_1(object sender, RoutedEventArgs e) 2 { 3 4 ExportDataGrid ...
- List泛型集合对象排序
本文的重点主要是解决:List<T>对象集合的排序功能. 一.List<T>.Sort 方法 () MSDN对这个无参Sort()方法的介绍:使用默认比较器对整个List< ...
- c# 内部类使用接口IComparer实现排序
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- IComparer 指定排序。
public class NeEntityComparer : IComparer<NeEntity> { public int Compare(NeEntity x, NeEntity ...
- Java对象比较器对泛型List进行排序-Demo
针对形如:字段1 字段2 字段3 字段n 1 hello 26 7891 world 89 5562 what 55 4562 the 85 452 fuck 55 995 haha 98 455 以 ...
- C# 中 List.Sort运用(IComparer<T>)排序用法
/// <summary> /// 比较人物类实例大小,实现接口IComparer /// </summary> public class InternetProtocolCo ...
- 泛型算法,排序的相关操作,lower_bound、upper_bound、equal_range
body, table{font-family: 微软雅黑; font-size: 13.5pt} table{border-collapse: collapse; border: solid gra ...
- [c#基础]泛型集合的自定义类型排序
引用 最近总有种感觉,自己复习的进度总被项目中的问题给耽搁了,项目中遇到的问题,不总结又不行,只能将复习基础方面的东西放后再放后.一直没研究过太深奥的东西,过去一年一直在基础上打转,写代码,反编译,不 ...
随机推荐
- Microsoft Office 2013 Product Key
Microsoft Office 2013 Product Key ( Professional Plus ) PGD67-JN23K-JGVWW-KTHP4-GXR9G B9GN2-DXXQC-9D ...
- 错误: 找不到或无法加载主类 Files\apache-activemq-5.10.0\bin\..\conf\login.config
在启动activemq的时候出现错误:“错误: 找不到或无法加载主类 Files\apache-activemq-5.10.0\bin\..\conf\login.config”,之前用activem ...
- phpize的深入理解
安装(fastcgi模式)的时候,常常有这样一句命令:/usr/local/webserver/php/bin/phpize一.phpize是干嘛的?phpize是什么东西呢?php官方的说明:htt ...
- Android实现推送方式解决方案(转)
本文介绍在Android中实现推送方式的基础知识及相关解决方案.推送功能在手机开发中应用的场景是越来起来了,不说别的,就我们手机上的新闻客户端就时不j时的推送过来新的消息,很方便的阅读最新的新闻信息. ...
- ubuntu-E:Encountered a section with no Package: header的解决办法
刚才打开ubuntu,我的版本是12.04.正想使用sudo apt-get install build-essential 时,出现了如下错误: E:Encountered a section wi ...
- Capturing Audio & Video in HTML5
使用HTML5抓取 Audio & Video 原文地址: http://www.html5rocks.com/en/tutorials/getusermedia/intro/ 本地化的文章: ...
- The shortest problem
The shortest problem Time Limit: 3000/1500 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others ...
- 棋盘问题 分类: 搜索 POJ 2015-08-09 13:02 4人阅读 评论(0) 收藏
棋盘问题 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 28474 Accepted: 14084 Description 在一 ...
- pip命令使用国内pypi镜像源加速在线安装
参考:http://www.cnblogs.com/yudar/p/4444097.html 用easy_install和pip来安装第三方库很方便 它们的原理其实就是从Python的官方源pypi. ...
- Camera 图像处理原理分析
1 前言 做为拍照手机的核心模块之一,camera sensor效果的调整,涉及到众多的参数,如果对基本的光学原理及sensor软/硬件对图像处理的原理能有深入的理解和把握的话,对我们 ...